P4C
The P4 Compiler
Loading...
Searching...
No Matches
symbol_table.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/* -*-C++-*- */
9
10#ifndef FRONTENDS_P4_SYMBOL_TABLE_H_
11#define FRONTENDS_P4_SYMBOL_TABLE_H_
12
13/* A very simple symbol table that recognizes types; necessary because
14 the v1.2 grammar is ambiguous without type information */
15
16#include <unordered_map>
17#include <vector>
18
19#include "ir/ir.h"
20#include "lib/cstring.h"
21#include "lib/source_file.h"
22
23namespace P4::Util {
24class NamedSymbol;
25class Namespace;
26
27// Maintains a high-level representation of the program structure
28// built on the fly during parsing. Used by the lexer to
29// disambiguate identifiers.
30class ProgramStructure final {
31 private:
32 bool debug;
33 FILE *debugStream;
34 Namespace *rootNamespace;
35 Namespace *currentNamespace;
36
37 struct PathContext {
38 const NamedSymbol *previousSymbol;
39 const Namespace *lookupContext;
40 PathContext() : previousSymbol(nullptr), lookupContext(nullptr) {}
41 } identifierContext;
42
43 void push(Namespace *ns);
44 NamedSymbol *lookup(const cstring identifier);
45 void declare(NamedSymbol *symbol);
46
47 public:
48 enum class SymbolKind {
49 Identifier,
50 TemplateIdentifier,
51 Type,
52 TemplateType,
53 };
54
55 ProgramStructure();
56
57 void setDebug(bool debug) { this->debug = debug; }
58 void pushNamespace(SourceInfo info, bool allowDuplicates);
59 void pushContainerType(IR::ID id, bool allowDuplicates);
60 void declareType(IR::ID id);
61 void declareObject(IR::ID id, cstring type);
62 void markAsTemplate(IR::ID id); // the symbol expects template args
63
64 // the last namespace has been exited
65 void pop();
66 // Declares these types in the current scope
67 void declareTypes(const IR::IndexedVector<IR::Type_Var> &typeVars);
68 void declareParameters(const IR::IndexedVector<IR::Parameter> &params);
69 SymbolKind lookupIdentifier(cstring identifier);
70
71 void startAbsolutePath();
72 void relativePathFromLastSymbol();
73 void clearPath();
74
75 void endParse();
76
77 cstring toString() const;
78 void clear();
79};
80
81} // namespace P4::Util
82#endif /* FRONTENDS_P4_SYMBOL_TABLE_H_ */
Definition indexed_vector.h:31
Definition symbol_table.cpp:19
Definition symbol_table.cpp:52
Definition source_file.h:123
Definition cstring.h:76
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:158
Definition id.h:19