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
64
65class PCBEXPR_UCODE final : public LIBEVAL::UCODE
66{
67public:
69 virtual ~PCBEXPR_UCODE() {};
70
71 virtual std::unique_ptr<LIBEVAL::VAR_REF> CreateVarRef( const wxString& aVar,
72 const wxString& aField ) override;
73 virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall( const wxString& aName ) override;
74
76 bool RequiresPairItems() const { return m_requiresPairItems; }
77
78private:
80 bool m_requiresPairItems = false;
81};
82
83
85{
86public:
87 PCBEXPR_CONTEXT( int aConstraint = 0, PCB_LAYER_ID aLayer = F_Cu ) :
88 m_constraint( aConstraint ),
89 m_layer( aLayer )
90 {
91 m_items[0] = nullptr;
92 m_items[1] = nullptr;
93 }
94
95 void SetItems( BOARD_ITEM* a, BOARD_ITEM* b = nullptr )
96 {
97 m_items[0] = a;
98 m_items[1] = b;
99 }
100
101 void SetConstraint( int aConstraint ) { m_constraint = aConstraint; }
102 void SetLayer( PCB_LAYER_ID aLayer ) { m_layer = aLayer; }
103
105 void Reset() override
106 {
108 m_constraint = 0;
109 m_layer = F_Cu;
110 m_items[0] = nullptr;
111 m_items[1] = nullptr;
112 m_typeOverrides.clear();
113 }
114
115 void SetTypeOverride( const BOARD_ITEM* aItem, KICAD_T aType ) { m_typeOverrides[aItem] = aType; }
116
117 KICAD_T GetEffectiveType( const BOARD_ITEM* aItem ) const;
118
119 BOARD* GetBoard() const;
120
121 int GetConstraint() const { return m_constraint; }
122 BOARD_ITEM* GetItem( int index ) const { return m_items[index]; }
123 PCB_LAYER_ID GetLayer() const { return m_layer; }
124
125private:
129 std::map<const BOARD_ITEM*, KICAD_T> m_typeOverrides;
130};
131
132
134{
135public:
136 PCBEXPR_VAR_REF( int aItemIndex ) :
137 m_itemIndex( aItemIndex ),
138 m_type( LIBEVAL::VT_UNDEFINED )
139 {
140 }
141
142 ~PCBEXPR_VAR_REF() override = default;
143
144 static PCBEXPR_PROPERTY_KIND ClassifyProperty( const PROPERTY_BASE* aProperty );
146
147 void SetType( LIBEVAL::VAR_TYPE_T type ) { m_type = type; }
148 LIBEVAL::VAR_TYPE_T GetType() const override { return m_type; }
149
150 void AddAllowedClass( TYPE_ID aTypeHash, PROPERTY_BASE* aProperty, PCBEXPR_PROPERTY_KIND aKind )
151 {
152 m_matchingTypes[aTypeHash] = { aProperty, aKind };
153 }
154
155 // Navigation steps walked from the base item before the property/method is resolved.
156 // An empty chain (the default) resolves the base item directly, as before.
157 void SetNavigation( std::vector<PCBEXPR_NAV_STEP> aNavigation )
158 {
159 m_navigation = std::move( aNavigation );
160 }
161
162 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
163
164 BOARD_ITEM* GetObject( const LIBEVAL::CONTEXT* aCtx ) const;
165
166private:
172
173 std::unordered_map<TYPE_ID, MATCHED_PROPERTY> m_matchingTypes;
176 std::vector<PCBEXPR_NAV_STEP> m_navigation;
177};
178
179
180// "Object code" version of a netclass reference (for performance).
182{
183public:
184 PCBEXPR_NETCLASS_REF( int aItemIndex ) :
185 PCBEXPR_VAR_REF( aItemIndex )
186 {
188 }
189
190 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
191};
192
193
194// "Object code" version of a component class reference (for performance).
196{
197public:
198 PCBEXPR_COMPONENT_CLASS_REF( int aItemIndex ) : PCBEXPR_VAR_REF( aItemIndex )
199 {
201 }
202
203 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
204};
205
206
207// "Object code" version of a netname reference (for performance).
209{
210public:
211 PCBEXPR_NETNAME_REF( int aItemIndex ) :
212 PCBEXPR_VAR_REF( aItemIndex )
213 {
215 }
216
217 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
218};
219
220
222{
223public:
224 PCBEXPR_TYPE_REF( int aItemIndex ) :
225 PCBEXPR_VAR_REF( aItemIndex )
226 {
228 }
229
230 LIBEVAL::VALUE* GetValue( LIBEVAL::CONTEXT* aCtx ) override;
231};
232
233
235{
236public:
238
240 {
241 static PCBEXPR_BUILTIN_FUNCTIONS self;
242 return self;
243 }
244
245 LIBEVAL::FUNC_CALL_REF Get( const wxString& name )
246 {
247 return m_funcs[ name ];
248 }
249
250 bool IsGeometryDependent( const wxString& name ) const
251 {
252 return m_geometryDependentFuncs.count( name ) > 0;
253 }
254
255 const wxArrayString GetSignatures() const
256 {
257 return m_funcSigs;
258 }
259
260 void RegisterFunc( const wxString& funcSignature, LIBEVAL::FUNC_CALL_REF funcPtr,
261 bool aIsGeometryDependent = false )
262 {
263 wxString funcName = funcSignature.BeforeFirst( '(' );
264 wxString lower = funcName.Lower();
265 m_funcs[std::string( lower )] = std::move( funcPtr );
266 m_funcSigs.Add( funcSignature );
267
268 if( aIsGeometryDependent )
269 m_geometryDependentFuncs.insert( lower );
270 }
271
273
274private:
275 std::map<wxString, LIBEVAL::FUNC_CALL_REF> m_funcs;
276 std::set<wxString> m_geometryDependentFuncs;
277
278 wxArrayString m_funcSigs;
279};
280
281
283{
284public:
285 const std::vector<wxString>& GetSupportedUnits() const override;
286 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
287
288 wxString GetSupportedUnitsMessage() const override;
289
290 double Convert( const wxString& aString, int unitId ) const override;
291};
292
293
295{
296public:
297 const std::vector<wxString>& GetSupportedUnits() const override;
298 const std::vector<EDA_UNITS>& GetSupportedUnitsTypes() const override;
299
300 double Convert( const wxString& aString, int unitId ) const override;
301};
302
303
305{
306public:
308};
309
310
312{
313public:
316
317 bool Evaluate( const wxString& aExpr );
318 int Result() const { return m_result; }
319 EDA_UNITS Units() const { return m_units; }
320
321 void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
322 {
323 m_compiler.SetErrorCallback( std::move( aCallback ) );
324 }
325
326 bool IsErrorPending() const { return m_errorStatus.pendingError; }
328
329private:
332
336};
337
338#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:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
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
bool RequiresPairItems() const
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
void AddAllowedClass(TYPE_ID aTypeHash, PROPERTY_BASE *aProperty, PCBEXPR_PROPERTY_KIND aKind)
static PCBEXPR_PROPERTY_KIND ClassifyProperty(const PROPERTY_BASE *aProperty)
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
LIBEVAL::VAR_TYPE_T GetType() const override
void SetType(LIBEVAL::VAR_TYPE_T type)
std::unordered_map< TYPE_ID, MATCHED_PROPERTY > m_matchingTypes
PCBEXPR_VAR_REF(int aItemIndex)
void SetNavigation(std::vector< PCBEXPR_NAV_STEP > aNavigation)
~PCBEXPR_VAR_REF() override=default
static LIBEVAL::VAR_TYPE_T ExpressionType(PCBEXPR_PROPERTY_KIND aKind)
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_PROPERTY_KIND
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