KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbexpr_evaluator.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#ifndef PCBEXPR_EVALUATOR_H
22#define PCBEXPR_EVALUATOR_H
23
24#include <set>
25#include <map>
26#include <unordered_map>
27#include <core/typeinfo.h>
28
29#include <layer_ids.h>
30
31#include <properties/property.h>
33
35
36class BOARD;
37class BOARD_ITEM;
38
39class PCBEXPR_VAR_REF;
40
41
42// A navigation step applied to a variable reference before its property or method is resolved.
43// "A.Parent.getField('x')" resolves item A, then steps to its parent before calling getField().
45{
47};
48
49class PCBEXPR_UCODE final : public LIBEVAL::UCODE
50{
51public:
53 virtual ~PCBEXPR_UCODE() {};
54
55 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
56 const wxString& aField ) override;
57 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
58
60
61private:
63};
64
65
67{
68public:
69 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
70 m_constraint( aConstraint ),
71 m_layer( aLayer )
72 {
73 m_items[0] = nullptr;
74 m_items[1] = nullptr;
75 }
76
77 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
78 {
79 m_items[0] = a;
80 m_items[1] = b;
81 }
82
83 void SetConstraint( int aConstraint ) { m_constraint = aConstraint; }
84 void SetLayer( PCB_LAYER_ID aLayer ) { m_layer = aLayer; }
85
87 void Reset() override
88 {
90 m_constraint = 0;
91 m_layer = F_Cu;
92 m_items[0] = nullptr;
93 m_items[1] = nullptr;
94 m_typeOverrides.clear();
95 }
96
97 void SetTypeOverride( const BOARD_ITEM* aItem, KICAD_T aType ) { m_typeOverrides[aItem] = aType; }
98
99 KICAD_T GetEffectiveType( const BOARD_ITEM* aItem ) const;
100
101 BOARD* GetBoard() const;
102
103 int GetConstraint() const { return m_constraint; }
104 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
105 PCB_LAYER_ID GetLayer() const { return m_layer; }
106
107private:
111 std::map<const BOARD_ITEM*, KICAD_T> m_typeOverrides;
112};
113
114
116{
117public:
118 PCBEXPR_VAR_REF( int aItemIndex ) :
119 m_itemIndex( aItemIndex ),
120 m_type( LIBEVAL::VT_UNDEFINED ),
121 m_isEnum( false ),
122 m_isOptional( false )
123 {}
124
126
127 void SetIsEnum( bool s ) { m_isEnum = s; }
128 bool IsEnum() const { return m_isEnum; }
129
130 void SetIsOptional( bool s = true ) { m_isOptional = s; }
131 bool IsOptional() const { return m_isOptional; }
132
133 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
134 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
135
136 void AddAllowedClass( TYPE_ID type_hash, PROPERTY_BASE* prop )
137 {
138 m_matchingTypes[type_hash] = prop;
139 }
140
141 // Navigation steps walked from the base item before the property/method is resolved.
142 // An empty chain (the default) resolves the base item directly, as before.
143 void SetNavigation( std::vector<PCBEXPR_NAV_STEP> aNavigation )
144 {
145 m_navigation = std::move( aNavigation );
146 }
147
148 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
149
150 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
151
152private:
153 std::unordered_map<TYPE_ID, PROPERTY_BASE*> m_matchingTypes;
158 std::vector<PCBEXPR_NAV_STEP> m_navigation;
159};
160
161
162// "Object code" version of a netclass reference (for performance).
164{
165public:
166 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
167 PCBEXPR_VAR_REF( aItemIndex )
168 {
170 }
171
172 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
173};
174
175
176// "Object code" version of a component class reference (for performance).
178{
179public:
180 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
181 {
183 }
184
185 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
186};
187
188
189// "Object code" version of a netname reference (for performance).
191{
192public:
193 PCBEXPR_NETNAME_REF( int aItemIndex ) :
194 PCBEXPR_VAR_REF( aItemIndex )
195 {
197 }
198
199 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
200};
201
202
204{
205public:
206 PCBEXPR_TYPE_REF( int aItemIndex ) :
207 PCBEXPR_VAR_REF( aItemIndex )
208 {
210 }
211
212 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
213};
214
215
217{
218public:
220
222 {
223 static PCBEXPR_BUILTIN_FUNCTIONS self;
224 return self;
225 }
226
227 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
228 {
229 return m_funcs[ name ];
230 }
231
232 bool IsGeometryDependent( const wxString& name ) const
233 {
234 return m_geometryDependentFuncs.count( name ) > 0;
235 }
236
237 const wxArrayString GetSignatures() const
238 {
239 return m_funcSigs;
240 }
241
242 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr,
243 bool aIsGeometryDependent = false )
244 {
245 wxString funcName = funcSignature.BeforeFirst( '(' );
246 wxString lower = funcName.Lower();
247 m_funcs[std::string( lower )] = std::move( funcPtr );
248 m_funcSigs.Add( funcSignature );
249
250 if( aIsGeometryDependent )
251 m_geometryDependentFuncs.insert( lower );
252 }
253
255
256private:
257 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
258 std::set<wxString> m_geometryDependentFuncs;
259
260 wxArrayString m_funcSigs;
261};
262
263
265{
266public:
267 const std::vector<wxString>& GetSupportedUnits() const override;
268 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
269
270 wxString GetSupportedUnitsMessage() const override;
271
272 double Convert( const wxString& aString, int unitId ) const override;
273};
274
275
277{
278public:
279 const std::vector<wxString>& GetSupportedUnits() const override;
280 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
281
282 double Convert( const wxString& aString, int unitId ) const override;
283};
284
285
287{
288public:
290};
291
292
294{
295public:
298
299 bool Evaluate( const wxString& aExpr );
300 int Result() const { return m_result; }
301 EDA_UNITS Units() const { return m_units; }
302
303 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
304 {
305 m_compiler.SetErrorCallback( std::move( aCallback ) );
306 }
307
308 bool IsErrorPending() const { return m_errorStatus.pendingError; }
310
311private:
314
318};
319
320#endif
int index
const char * name
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
virtual void Reset()
Release every value allocated by the previous evaluation and rewind the stack so the same CONTEXT can...
bool IsGeometryDependent(const wxString &name) const
const wxArrayString GetSignatures() const
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
std::map< wxString, LIBEVAL::FUNC_CALL_REF > m_funcs
static PCBEXPR_BUILTIN_FUNCTIONS & Instance()
void RegisterFunc(const wxString &funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr, bool aIsGeometryDependent=false)
std::set< wxString > m_geometryDependentFuncs
PCBEXPR_COMPILER(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPONENT_CLASS_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
void SetConstraint(int aConstraint)
void SetLayer(PCB_LAYER_ID aLayer)
BOARD_ITEM * m_items[2]
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
PCBEXPR_CONTEXT(int aConstraint=0, PCB_LAYER_ID aLayer=F_Cu)
BOARD * GetBoard() const
int GetConstraint() const
void Reset() override
Rewind for reuse on the next evaluation (see LIBEVAL::CONTEXT::Reset()).
KICAD_T GetEffectiveType(const BOARD_ITEM *aItem) const
PCB_LAYER_ID GetLayer() const
void SetTypeOverride(const BOARD_ITEM *aItem, KICAD_T aType)
std::map< const BOARD_ITEM *, KICAD_T > m_typeOverrides
BOARD_ITEM * GetItem(int index) const
LIBEVAL::ERROR_STATUS m_errorStatus
bool IsErrorPending() const
PCBEXPR_EVALUATOR(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPILER m_compiler
const LIBEVAL::ERROR_STATUS & GetError() const
bool Evaluate(const wxString &aExpr)
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
EDA_UNITS Units() const
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
PCBEXPR_NETCLASS_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
PCBEXPR_NETNAME_REF(int aItemIndex)
PCBEXPR_TYPE_REF(int aItemIndex)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
virtual std::unique_ptr< LIBEVAL::VAR_REF > CreateVarRef(const wxString &aVar, const wxString &aField) override
bool HasGeometryDependentFunctions() const
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
virtual ~PCBEXPR_UCODE()
bool m_hasGeometryDependentFunctions
const std::vector< wxString > & GetSupportedUnits() const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
double Convert(const wxString &aString, int unitId) const override
double Convert(const wxString &aString, int unitId) const override
const std::vector< EDA_UNITS > & GetSupportedUnitsTypes() const override
wxString GetSupportedUnitsMessage() const override
const std::vector< wxString > & GetSupportedUnits() const override
std::vector< PCBEXPR_NAV_STEP > m_navigation
std::unordered_map< TYPE_ID, PROPERTY_BASE * > m_matchingTypes
void SetIsEnum(bool s)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
LIBEVAL::VAR_TYPE_T GetType() const override
bool IsOptional() const
void SetType(LIBEVAL::VAR_TYPE_T type)
void SetIsOptional(bool s=true)
PCBEXPR_VAR_REF(int aItemIndex)
void SetNavigation(std::vector< PCBEXPR_NAV_STEP > aNavigation)
void AddAllowedClass(TYPE_ID type_hash, PROPERTY_BASE *prop)
LIBEVAL::VAR_TYPE_T m_type
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
EDA_UNITS
Definition eda_units.h:44
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Cu
Definition layer_ids.h:60
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
PCBEXPR_NAV_STEP
size_t TYPE_ID
Unique type identifier.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:71