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 <mutex>
28#include <board.h>
30#include <pcbexpr_evaluator.h>
31#include <drc/drc_engine.h>
32
33/* --------------------------------------------------------------------------------------------
34 * Specialized Expression References
35 */
36
38{
39 wxASSERT( dynamic_cast<const PCBEXPR_CONTEXT*>( aCtx ) );
40
41 const PCBEXPR_CONTEXT* ctx = static_cast<const PCBEXPR_CONTEXT*>( aCtx );
42 BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
43 return item;
44}
45
46
48{
49public:
51 LIBEVAL::VALUE( LayerName( aLayer ) ),
52 m_layer( aLayer )
53 {};
54
55 virtual bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
56 {
57 // For boards with user-defined layer names there will be 2 entries for each layer
58 // in the ENUM_MAP: one for the canonical layer name and one for the user layer name.
59 // We need to check against both.
60
61 wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
62 const wxString& layerName = b->AsString();
63 BOARD* board = static_cast<PCBEXPR_CONTEXT*>( aCtx )->GetBoard();
64
65 {
66 std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
67
68 auto i = board->m_LayerExpressionCache.find( layerName );
69
70 if( i != board->m_LayerExpressionCache.end() )
71 return i->second.Contains( m_layer );
72 }
73
74 LSET mask;
75
76 for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
77 {
78 wxPGChoiceEntry& entry = layerMap[ii];
79
80 if( entry.GetText().Matches( layerName ) )
81 mask.set( ToLAYER_ID( entry.GetValue() ) );
82 }
83
84 {
85 std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
86 board->m_LayerExpressionCache[ layerName ] = mask;
87 }
88
89 return mask.Contains( m_layer );
90 }
91
92protected:
94};
95
96
98{
99public:
100 PCBEXPR_PINTYPE_VALUE( const wxString& aPinTypeName ) :
101 LIBEVAL::VALUE( aPinTypeName )
102 {};
103
104 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
105 {
106 const wxString& thisStr = AsString();
107 const wxString& otherStr = b->AsString();
108
109 if( thisStr.IsSameAs( otherStr, false ) )
110 return true;
111
112 // Handle cases where the netlist token is different from the EEschema token
113 if( thisStr == wxT( "tri_state" ) )
114 return otherStr.IsSameAs( wxT( "Tri-state" ), false );
115
116 if( thisStr == wxT( "power_in" ) )
117 return otherStr.IsSameAs( wxT( "Power input" ), false );
118
119 if( thisStr == wxT( "power_out" ) )
120 return otherStr.IsSameAs( wxT( "Power output" ), false );
121
122 if( thisStr == wxT( "no_connect" ) )
123 return otherStr.IsSameAs( wxT( "Unconnected" ), false );
124
125 return false;
126 }
127};
128
129
131{
132public:
134 LIBEVAL::VALUE( wxEmptyString ),
135 m_item( aItem )
136 {};
137
138 const wxString& AsString() const override
139 {
140 const_cast<PCBEXPR_NETCLASS_VALUE*>( this )->Set( m_item->GetEffectiveNetClass()->GetName() );
142 }
143
144 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
145 {
146 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
147 return m_item->GetEffectiveNetClass() == bValue->m_item->GetEffectiveNetClass();
148 else
149 return LIBEVAL::VALUE::EqualTo( aCtx, b );
150 }
151
152 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
153 {
154 if( const PCBEXPR_NETCLASS_VALUE* bValue = dynamic_cast<const PCBEXPR_NETCLASS_VALUE*>( b ) )
155 return m_item->GetEffectiveNetClass() != bValue->m_item->GetEffectiveNetClass();
156 else
157 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
158 }
159
160protected:
162};
163
164
166{
167public:
169 LIBEVAL::VALUE( wxEmptyString ),
170 m_item( aItem )
171 {};
172
173 const wxString& AsString() const override
174 {
175 const_cast<PCBEXPR_NET_VALUE*>( this )->Set( m_item->GetNetname() );
177 }
178
179 bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override
180 {
181 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
182 return m_item->GetNetCode() == bValue->m_item->GetNetCode();
183 else
184 return LIBEVAL::VALUE::EqualTo( aCtx, b );
185 }
186
187 bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override
188 {
189 if( const PCBEXPR_NET_VALUE* bValue = dynamic_cast<const PCBEXPR_NET_VALUE*>( b ) )
190 return m_item->GetNetCode() != bValue->m_item->GetNetCode();
191 else
192 return LIBEVAL::VALUE::NotEqualTo( aCtx, b );
193 }
194
195protected:
197};
198
199
201{
202 PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
203
204 if( m_itemIndex == 2 )
205 return new PCBEXPR_LAYER_VALUE( context->GetLayer() );
206
207 BOARD_ITEM* item = GetObject( aCtx );
208
209 if( !item )
210 return new LIBEVAL::VALUE();
211
212 auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
213
214 if( it == m_matchingTypes.end() )
215 {
216 // Don't force user to type "A.Type == 'via' && A.Via_Type == 'buried'" when the
217 // simpler "A.Via_Type == 'buried'" is perfectly clear. Instead, return an undefined
218 // value when the property doesn't appear on a particular object.
219
220 return new LIBEVAL::VALUE();
221 }
222 else
223 {
225 {
226 return new LIBEVAL::VALUE( (double) item->Get<int>( it->second ) );
227 }
228 else
229 {
230 wxString str;
231
232 if( !m_isEnum )
233 {
234 str = item->Get<wxString>( it->second );
235
236 if( it->second->Name() == wxT( "Pin Type" ) )
237 return new PCBEXPR_PINTYPE_VALUE( str );
238 else
239 return new LIBEVAL::VALUE( str );
240 }
241 else
242 {
243 const wxAny& any = item->Get( it->second );
244 PCB_LAYER_ID layer;
245
246 if( it->second->Name() == wxT( "Layer" )
247 || it->second->Name() == wxT( "Layer Top" )
248 || it->second->Name() == wxT( "Layer Bottom" ) )
249 {
250 if( any.GetAs<PCB_LAYER_ID>( &layer ) )
251 return new PCBEXPR_LAYER_VALUE( layer );
252 else if( any.GetAs<wxString>( &str ) )
253 return new PCBEXPR_LAYER_VALUE( context->GetBoard()->GetLayerID( str ) );
254 }
255 else
256 {
257 if( any.GetAs<wxString>( &str ) )
258 return new LIBEVAL::VALUE( str );
259 }
260 }
261
262 return new LIBEVAL::VALUE();
263 }
264 }
265}
266
267
269{
270 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
271
272 if( !item )
273 return new LIBEVAL::VALUE();
274
275 return new PCBEXPR_NETCLASS_VALUE( item );
276}
277
278
280{
281 BOARD_CONNECTED_ITEM* item = dynamic_cast<BOARD_CONNECTED_ITEM*>( GetObject( aCtx ) );
282
283 if( !item )
284 return new LIBEVAL::VALUE();
285
286 return new PCBEXPR_NET_VALUE( item );
287}
288
289
291{
292 BOARD_ITEM* item = GetObject( aCtx );
293
294 if( !item )
295 return new LIBEVAL::VALUE();
296
297 return new LIBEVAL::VALUE( ENUM_MAP<KICAD_T>::Instance().ToString( item->Type() ) );
298}
299
300
302{
304
305 return registry.Get( aName.Lower() );
306}
307
308
309std::unique_ptr<LIBEVAL::VAR_REF> PCBEXPR_UCODE::CreateVarRef( const wxString& aVar,
310 const wxString& aField )
311{
313 std::unique_ptr<PCBEXPR_VAR_REF> vref;
314
315 // Check for a couple of very common cases and compile them straight to "object code".
316
317 if( aField.CmpNoCase( wxT( "NetClass" ) ) == 0 )
318 {
319 if( aVar == wxT( "A" ) )
320 return std::make_unique<PCBEXPR_NETCLASS_REF>( 0 );
321 else if( aVar == wxT( "B" ) )
322 return std::make_unique<PCBEXPR_NETCLASS_REF>( 1 );
323 else
324 return nullptr;
325 }
326 else if( aField.CmpNoCase( wxT( "NetName" ) ) == 0 )
327 {
328 if( aVar == wxT( "A" ) )
329 return std::make_unique<PCBEXPR_NETNAME_REF>( 0 );
330 else if( aVar == wxT( "B" ) )
331 return std::make_unique<PCBEXPR_NETNAME_REF>( 1 );
332 else
333 return nullptr;
334 }
335 else if( aField.CmpNoCase( wxT( "Type" ) ) == 0 )
336 {
337 if( aVar == wxT( "A" ) )
338 return std::make_unique<PCBEXPR_TYPE_REF>( 0 );
339 else if( aVar == wxT( "B" ) )
340 return std::make_unique<PCBEXPR_TYPE_REF>( 1 );
341 else
342 return nullptr;
343 }
344
345 if( aVar == wxT( "A" ) || aVar == wxT( "AB" ) )
346 vref = std::make_unique<PCBEXPR_VAR_REF>( 0 );
347 else if( aVar == wxT( "B" ) )
348 vref = std::make_unique<PCBEXPR_VAR_REF>( 1 );
349 else if( aVar == wxT( "L" ) )
350 vref = std::make_unique<PCBEXPR_VAR_REF>( 2 );
351 else
352 return nullptr;
353
354 if( aField.length() == 0 ) // return reference to base object
355 {
356 return vref;
357 }
358
359 wxString field( aField );
360 field.Replace( wxT( "_" ), wxT( " " ) );
361
362 for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
363 {
364 if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
365 {
366 PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
367
368 if( prop )
369 {
370 vref->AddAllowedClass( cls.type, prop );
371
372 if( prop->TypeHash() == TYPE_HASH( int ) )
373 {
374 vref->SetType( LIBEVAL::VT_NUMERIC );
375 }
376 else if( prop->TypeHash() == TYPE_HASH( bool ) )
377 {
378 vref->SetType( LIBEVAL::VT_NUMERIC );
379 }
380 else if( prop->TypeHash() == TYPE_HASH( wxString ) )
381 {
382 vref->SetType( LIBEVAL::VT_STRING );
383 }
384 else if ( prop->HasChoices() )
385 { // it's an enum, we treat it as string
386 vref->SetType( LIBEVAL::VT_STRING );
387 vref->SetIsEnum ( true );
388 }
389 else
390 {
391 wxFAIL_MSG( wxT( "PCBEXPR_UCODE::createVarRef: Unknown property type." ) );
392 }
393 }
394 }
395 }
396
397 if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
398 vref->SetType( LIBEVAL::VT_PARSE_ERROR );
399
400 return vref;
401}
402
403
405{
406 if( m_items[0] )
407 return m_items[0]->GetBoard();
408
409 return nullptr;
410}
411
412
413/* --------------------------------------------------------------------------------------------
414 * Unit Resolvers
415 */
416
417const std::vector<wxString>& PCBEXPR_UNIT_RESOLVER::GetSupportedUnits() const
418{
419 static const std::vector<wxString> pcbUnits = { wxT( "mil" ), wxT( "mm" ), wxT( "in" ) };
420
421 return pcbUnits;
422}
423
424
426{
427 return _( "must be mm, in, or mil" );
428}
429
430
431double PCBEXPR_UNIT_RESOLVER::Convert( const wxString& aString, int unitId ) const
432{
433 double v = wxAtof( aString );
434
435 switch( unitId )
436 {
437 case 0: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILS, aString );
438 case 1: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILLIMETRES, aString );
439 case 2: return EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::INCHES, aString );
440 default: return v;
441 }
442};
443
444
445const std::vector<wxString>& PCBEXPR_UNITLESS_RESOLVER::GetSupportedUnits() const
446{
447 static const std::vector<wxString> emptyUnits;
448
449 return emptyUnits;
450}
451
452
453double PCBEXPR_UNITLESS_RESOLVER::Convert( const wxString& aString, int unitId ) const
454{
455 return wxAtof( aString );
456};
457
458
460{
461 m_unitResolver.reset( aUnitResolver );
462}
463
464
465/* --------------------------------------------------------------------------------------------
466 * PCB Expression Evaluator
467 */
468
470 m_result( 0 ),
471 m_compiler( aUnitResolver ),
472 m_ucode(),
473 m_errorStatus()
474{
475}
476
477
479{
480}
481
482
483bool PCBEXPR_EVALUATOR::Evaluate( const wxString& aExpr )
484{
485 PCBEXPR_UCODE ucode;
486 PCBEXPR_CONTEXT preflightContext( NULL_CONSTRAINT, F_Cu );
487
488 if( !m_compiler.Compile( aExpr.ToUTF8().data(), &ucode, &preflightContext ) )
489 return false;
490
491 PCBEXPR_CONTEXT evaluationContext( NULL_CONSTRAINT, F_Cu );
492 LIBEVAL::VALUE* result = ucode.Run( &evaluationContext );
493
494 if( result->GetType() == LIBEVAL::VT_NUMERIC )
495 m_result = KiROUND( result->AsDouble() );
496
497 return true;
498}
499
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
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:46
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
PCB_LAYER_ID GetLayerID(const wxString &aLayerName) const
Return the ID of a layer.
Definition: board.cpp:547
std::unordered_map< wxString, LSET > m_LayerExpressionCache
Definition: board.h:1268
std::shared_mutex m_CachesMutex
Definition: board.h:1262
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:100
static ENUM_MAP< T > & Instance()
Definition: property.h:663
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:99
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:575
bool Contains(PCB_LAYER_ID aLayer)
See if the layer set contains a PCB layer.
Definition: layer_ids.h:647
const wxString GetName() const
Definition: netclass.h:62
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
bool EqualTo(LIBEVAL::CONTEXT *aCtx, const VALUE *b) const override
PCBEXPR_PINTYPE_VALUE(const wxString &aPinTypeName)
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:241
Provide class metadata.Helper macro to map type hashes to names.
Definition: property_mgr.h:85
CLASSES_INFO GetAllClasses()
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
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:64
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:1022
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:572
std::function< void(CONTEXT *, void *)> FUNC_CALL_REF
BOARD * GetBoard()
#define TYPE_HASH(x)
Definition: property.h:71
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:118