KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbexpr_evaluator.cpp
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 (C) 2019-2022 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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#include <cstdio>
26#include <memory>
27#include <board.h>
29#include <pcbexpr_evaluator.h>
30#include <drc/drc_engine.h>
31
32/* --------------------------------------------------------------------------------------------
33 * Specialized Expression References
34 */
35
37{
38 wxASSERT( dynamic_cast<const PCBEXPR_CONTEXT*>( aCtx ) );
39
40 const PCBEXPR_CONTEXT* ctx = static_cast<const PCBEXPR_CONTEXT*>( aCtx );
41 BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
42 return item;
43}
44
45
47{
48public:
50 LIBEVAL::VALUE( LayerName( aLayer ) ),
51 m_layer( aLayer )
52 {};
53
54 virtual bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
55 {
56 // For boards with user-defined layer names there will be 2 entries for each layer
57 // in the ENUM_MAP: one for the canonical layer name and one for the user layer name.
58 // We need to check against both.
59
60 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
61 const wxString& layerName = b->AsString();
62 BOARD* board = static_cast<PCBEXPR_CONTEXT*>( aCtx )->GetBoard();
63 std::unique_lock<std::mutex> cacheLock( board->m_CachesMutex );
64 auto i = board->m_LayerExpressionCache.find( layerName );
65 LSET mask;
66
67 if( i == board->m_LayerExpressionCache.end() )
68 {
69 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
70 {
71 wxPGChoiceEntry& entry = layerMap[ii];
72
73 if( entry.GetText().Matches( layerName ) )
74 mask.set( ToLAYER_ID( entry.GetValue() ) );
75 }
76
77 board->m_LayerExpressionCache[ layerName ] = mask;
78 }
79 else
80 {
81 mask = i->second;
82 }
83
84 return mask.Contains( m_layer );
85 }
86
87protected:
89};
90
91
93{
94public:
96 LIBEVAL::VALUE( wxEmptyString ),
97 m_item( aItem )
98 {};
99
100 const wxString& AsString() const override
101 {
102 const_cast<PCBEXPR_NETCLASS_VALUE*>( this )->Set( m_item->GetEffectiveNetClass()->GetName() );
104 }
105
106 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
107 {
108 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
109 return m_item->GetEffectiveNetClass() == bValue->m_item->GetEffectiveNetClass();
110 else
111 return LIBEVAL::VALUE::EqualTo( aCtx, b );
112 }
113
114 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
115 {
116 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
117 return m_item->GetEffectiveNetClass() != bValue->m_item->GetEffectiveNetClass();
118 else
119 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
120 }
121
122protected:
124};
125
126
128{
129public:
131 LIBEVAL::VALUE( wxEmptyString ),
132 m_item( aItem )
133 {};
134
135 const wxString& AsString() const override
136 {
137 const_cast<PCBEXPR_NET_VALUE*>( this )->Set( m_item->GetNetname() );
139 }
140
141 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
142 {
143 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
144 return m_item->GetNetCode() == bValue->m_item->GetNetCode();
145 else
146 return LIBEVAL::VALUE::EqualTo( aCtx, b );
147 }
148
149 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
150 {
151 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
152 return m_item->GetNetCode() != bValue->m_item->GetNetCode();
153 else
154 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
155 }
156
157protected:
159};
160
161
163{
164 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
165
166 if( m_itemIndex == 2 )
167 return new PCBEXPR_LAYER_VALUE( context->GetLayer() );
168
169 BOARD_ITEM* item = GetObject( aCtx );
170
171 if( !item )
172 return new LIBEVAL::VALUE();
173
174 auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
175
176 if( it == m_matchingTypes.end() )
177 {
178 // Don't force user to type "A.Type == 'via' && A.Via_Type == 'buried'" when the
179 // simpler "A.Via_Type == 'buried'" is perfectly clear. Instead, return an undefined
180 // value when the property doesn't appear on a particular object.
181
182 return new LIBEVAL::VALUE();
183 }
184 else
185 {
187 return new LIBEVAL::VALUE( (double) item->Get<int>( it->second ) );
188 else
189 {
190 wxString str;
191
192 if( !m_isEnum )
193 {
194 str = item->Get<wxString>( it->second );
195 return new LIBEVAL::VALUE( str );
196 }
197 else
198 {
199 const wxAny& any = item->Get( it->second );
200 PCB_LAYER_ID layer;
201
202 if( it->second->Name() == wxT( "Layer" )
203 || it->second->Name() == wxT( "Layer Top" )
204 || it->second->Name() == wxT( "Layer Bottom" ) )
205 {
206 if( any.GetAs<PCB_LAYER_ID>( &layer ) )
207 return new PCBEXPR_LAYER_VALUE( layer );
208 else if( any.GetAs<wxString>( &str ) )
209 return new PCBEXPR_LAYER_VALUE( context->GetBoard()->GetLayerID( str ) );
210 }
211 else
212 {
213 if( any.GetAs<wxString>( &str ) )
214 return new LIBEVAL::VALUE( str );
215 }
216 }
217
218 return new LIBEVAL::VALUE();
219 }
220 }
221}
222
223
225{
226 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
227
228 if( !item )
229 return new LIBEVAL::VALUE();
230
231 return new PCBEXPR_NETCLASS_VALUE( item );
232}
233
234
236{
237 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
238
239 if( !item )
240 return new LIBEVAL::VALUE();
241
242 return new PCBEXPR_NET_VALUE( item );
243}
244
245
247{
248 BOARD_ITEM* item = GetObject( aCtx );
249
250 if( !item )
251 return new LIBEVAL::VALUE();
252
253 return new LIBEVAL::VALUE( ENUM_MAP<KICAD_T>::Instance().ToString( item->Type() ) );
254}
255
256
258{
260
261 return registry.Get( aName.Lower() );
262}
263
264
265std::unique_ptr<LIBEVAL::VAR_REF> PCBEXPR_UCODE::CreateVarRef( const wxString& aVar,
266 const wxString& aField )
267{
269 std::unique_ptr<PCBEXPR_VAR_REF> vref;
270
271 // Check for a couple of very common cases and compile them straight to "object code".
272
273 if( aField.CmpNoCase( wxT( "NetClass" ) ) == 0 )
274 {
275 if( aVar == wxT( "A" ) )
276 return std::make_unique<PCBEXPR_NETCLASS_REF>( 0 );
277 else if( aVar == wxT( "B" ) )
278 return std::make_unique<PCBEXPR_NETCLASS_REF>( 1 );
279 else
280 return nullptr;
281 }
282 else if( aField.CmpNoCase( wxT( "NetName" ) ) == 0 )
283 {
284 if( aVar == wxT( "A" ) )
285 return std::make_unique<PCBEXPR_NETNAME_REF>( 0 );
286 else if( aVar == wxT( "B" ) )
287 return std::make_unique<PCBEXPR_NETNAME_REF>( 1 );
288 else
289 return nullptr;
290 }
291 else if( aField.CmpNoCase( wxT( "Type" ) ) == 0 )
292 {
293 if( aVar == wxT( "A" ) )
294 return std::make_unique<PCBEXPR_TYPE_REF>( 0 );
295 else if( aVar == wxT( "B" ) )
296 return std::make_unique<PCBEXPR_TYPE_REF>( 1 );
297 else
298 return nullptr;
299 }
300
301 if( aVar == wxT( "A" ) || aVar == wxT( "AB" ) )
302 vref = std::make_unique<PCBEXPR_VAR_REF>( 0 );
303 else if( aVar == wxT( "B" ) )
304 vref = std::make_unique<PCBEXPR_VAR_REF>( 1 );
305 else if( aVar == wxT( "L" ) )
306 vref = std::make_unique<PCBEXPR_VAR_REF>( 2 );
307 else
308 return nullptr;
309
310 if( aField.length() == 0 ) // return reference to base object
311 {
312 return std::move( vref );
313 }
314
315 wxString field( aField );
316 field.Replace( wxT( "_" ), wxT( " " ) );
317
318 for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
319 {
320 if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
321 {
322 PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
323
324 if( prop )
325 {
326 vref->AddAllowedClass( cls.type, prop );
327
328 if( prop->TypeHash() == TYPE_HASH( int ) )
329 {
330 vref->SetType( LIBEVAL::VT_NUMERIC );
331 }
332 else if( prop->TypeHash() == TYPE_HASH( bool ) )
333 {
334 vref->SetType( LIBEVAL::VT_NUMERIC );
335 }
336 else if( prop->TypeHash() == TYPE_HASH( wxString ) )
337 {
338 vref->SetType( LIBEVAL::VT_STRING );
339 }
340 else if ( prop->HasChoices() )
341 { // it's an enum, we treat it as string
342 vref->SetType( LIBEVAL::VT_STRING );
343 vref->SetIsEnum ( true );
344 }
345 else
346 {
347 wxFAIL_MSG( wxT( "PCBEXPR_UCODE::createVarRef: Unknown property type." ) );
348 }
349 }
350 }
351 }
352
353 if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
354 vref->SetType( LIBEVAL::VT_PARSE_ERROR );
355
356 return std::move( vref );
357}
358
359
361{
362 if( m_items[0] )
363 return m_items[0]->GetBoard();
364
365 return nullptr;
366}
367
368
369/* --------------------------------------------------------------------------------------------
370 * Unit Resolvers
371 */
372
373const std::vector<wxString>& PCBEXPR_UNIT_RESOLVER::GetSupportedUnits() const
374{
375 static const std::vector<wxString> pcbUnits = { wxT( "mil" ), wxT( "mm" ), wxT( "in" ) };
376
377 return pcbUnits;
378}
379
380
382{
383 return _( "must be mm, in, or mil" );
384}
385
386
387double PCBEXPR_UNIT_RESOLVER::Convert( const wxString& aString, int unitId ) const
388{
389 double v = wxAtof( aString );
390
391 switch( unitId )
392 {
393 case 0: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILS, aString );
394 case 1: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILLIMETRES, aString );
395 case 2: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::INCHES, aString );
396 default: return v;
397 }
398};
399
400
401const std::vector<wxString>& PCBEXPR_UNITLESS_RESOLVER::GetSupportedUnits() const
402{
403 static const std::vector<wxString> emptyUnits;
404
405 return emptyUnits;
406}
407
408
409double PCBEXPR_UNITLESS_RESOLVER::Convert( const wxString& aString, int unitId ) const
410{
411 return wxAtof( aString );
412};
413
414
416{
417 m_unitResolver.reset( aUnitResolver );
418}
419
420
421/* --------------------------------------------------------------------------------------------
422 * PCB Expression Evaluator
423 */
424
426 m_result( 0 ),
427 m_compiler( aUnitResolver ),
428 m_ucode(),
429 m_errorStatus()
430{
431}
432
433
435{
436}
437
438
439bool PCBEXPR_EVALUATOR::Evaluate( const wxString& aExpr )
440{
441 PCBEXPR_UCODE ucode;
442 PCBEXPR_CONTEXT preflightContext( NULL_CONSTRAINT, F_Cu );
443
444 if( !m_compiler.Compile( aExpr.ToUTF8().data(), &ucode, &preflightContext ) )
445 return false;
446
447 PCBEXPR_CONTEXT evaluationContext( NULL_CONSTRAINT, F_Cu );
448 LIBEVAL::VALUE* result = ucode.Run( &evaluationContext );
449
450 if( result->GetType() == LIBEVAL::VT_NUMERIC )
451 m_result = KiROUND( result->AsDouble() );
452
453 return true;
454}
455
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
virtual NETCLASS * GetEffectiveNetClass() const
Return the NETCLASS for this item.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:45
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
PCB_LAYER_ID GetLayerID(const wxString &aLayerName) const
Return the ID of a layer.
Definition: board.cpp:495
std::unordered_map< wxString, LSET > m_LayerExpressionCache
Definition: board.h:1231
std::mutex m_CachesMutex
Definition: board.h:1225
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
static ENUM_MAP< T > & Instance()
Definition: property.h:640
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:84
std::unique_ptr< UNIT_RESOLVER > m_unitResolver
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
VALUE * Run(CONTEXT *ctx)
void Set(double aValue)
virtual const wxString & AsString() const
virtual bool NotEqualTo(CONTEXT *aCtx, const VALUE *b) const
virtual double AsDouble() const
VAR_TYPE_T GetType() const
virtual bool EqualTo(CONTEXT *aCtx, const VALUE *b) const
LSET is a set of PCB_LAYER_IDs.
Definition: layer_ids.h:556
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: layer_ids.h:628
const wxString GetName() const
Definition: netclass.h:65
LIBEVAL::FUNC_CALL_REF Get(const wxString &name)
static PCBEXPR_BUILTIN_FUNCTIONS & Instance()
PCBEXPR_COMPILER(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
BOARD_ITEM * m_items[2]
BOARD * GetBoard() const
PCB_LAYER_ID GetLayer() const
BOARD_ITEM * GetItem(int index) const
PCBEXPR_EVALUATOR(LIBEVAL::UNIT_RESOLVER *aUnitResolver)
PCBEXPR_COMPILER m_compiler
bool Evaluate(const wxString &aExpr)
PCBEXPR_LAYER_VALUE(PCB_LAYER_ID aLayer)
virtual bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
BOARD_CONNECTED_ITEM * m_item
PCBEXPR_NETCLASS_VALUE(BOARD_CONNECTED_ITEM *aItem)
const wxString & AsString() const override
bool NotEqualTo(LIBEVAL::CONTEXT *aCtx, const LIBEVAL::VALUE *b) const override
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
BOARD_CONNECTED_ITEM * m_item
PCBEXPR_NET_VALUE(BOARD_CONNECTED_ITEM *aItem)
bool NotEqualTo(LIBEVAL::CONTEXT *aCtx, const LIBEVAL::VALUE *b) const override
const wxString & AsString() const override
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
virtual std::unique_ptr< LIBEVAL::VAR_REF > CreateVarRef(const wxString &aVar, const wxString &aField) override
virtual LIBEVAL::FUNC_CALL_REF CreateFuncCall(const wxString &aName) override
const std::vector< wxString > & GetSupportedUnits() const override
double Convert(const wxString &aString, int unitId) const override
double Convert(const wxString &aString, int unitId) const override
wxString GetSupportedUnitsMessage() const override
const std::vector< wxString > & GetSupportedUnits() const override
std::unordered_map< TYPE_ID, PROPERTY_BASE * > m_matchingTypes
LIBEVAL::VALUE * GetValue(LIBEVAL::CONTEXT *aCtx) override
LIBEVAL::VAR_TYPE_T m_type
BOARD_ITEM * GetObject(const LIBEVAL::CONTEXT *aCtx) const
virtual size_t TypeHash() const =0
Return type-id of the property type.
virtual bool HasChoices() const
Return true if this PROPERTY has a limited set of possible values.
Definition: property.h:233
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:74
CLASSES_INFO GetAllClasses()
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:76
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
bool IsOfType(TYPE_ID aDerived, TYPE_ID aBase) const
Return true if aDerived is inherited from aBase.
@ NULL_CONSTRAINT
Definition: drc_rule.h:46
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:30
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ F_Cu
Definition: layer_ids.h:65
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:941
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:565
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
BOARD * GetBoard()
#define TYPE_HASH(x)
Definition: property.h:64
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85