@@ -115,7 +115,7 @@ internal LocationInfo GetLoc(ClassDefinition node) {
115
115
private LocationInfo GetLoc ( Node node ) => _scope . GetLoc ( node ) ;
116
116
117
117
private IMember Clone ( IMember member ) =>
118
- member is IPythonMultipleMembers mm ? AstPythonMultipleMembers . Create ( mm . Members ) :
118
+ member is IPythonMultipleMembers mm ? AstPythonMultipleMembers . Create ( mm . GetMembers ( ) ) :
119
119
member ;
120
120
121
121
public override bool Walk ( AssignmentStatement node ) {
@@ -423,14 +423,18 @@ public override bool Walk(FunctionDefinition node) {
423
423
var dec = ( node . Decorators ? . Decorators ) . MaybeEnumerate ( ) . ExcludeDefault ( ) ;
424
424
foreach ( var d in dec ) {
425
425
var obj = _scope . GetValueFromExpression ( d ) ;
426
+
427
+ var declaringType = obj as IPythonType ;
428
+ var declaringModule = declaringType ? . DeclaringModule ;
429
+
426
430
if ( obj == _interpreter . GetBuiltinType ( BuiltinTypeId . Property ) ) {
427
- AddProperty ( node ) ;
431
+ AddProperty ( node , declaringModule , declaringType ) ;
428
432
return false ;
429
433
}
430
- var mod = ( obj as IPythonType ) ? . DeclaringModule ?? ( obj as IPythonFunction ) ? . DeclaringModule ;
431
- var name = ( obj as IPythonType ) ? . Name ?? ( obj as IPythonFunction ) ? . Name ;
432
- if ( mod ? . Name == "abc" && name == "abstractproperty" ) {
433
- AddProperty ( node ) ;
434
+
435
+ var name = declaringType ? . Name ;
436
+ if ( declaringModule ? . Name == "abc" && name == "abstractproperty" ) {
437
+ AddProperty ( node , declaringModule , declaringType ) ;
434
438
return false ;
435
439
}
436
440
}
@@ -450,10 +454,10 @@ public override bool Walk(FunctionDefinition node) {
450
454
return false ;
451
455
}
452
456
453
- private void AddProperty ( FunctionDefinition node ) {
457
+ private void AddProperty ( FunctionDefinition node , IPythonModule declaringModule , IPythonType declaringType ) {
454
458
var existing = _scope . LookupNameInScopes ( node . Name , NameLookupContext . LookupOptions . Local ) as AstPythonProperty ;
455
459
if ( existing == null ) {
456
- existing = new AstPythonProperty ( _ast , node , GetLoc ( node ) ) ;
460
+ existing = new AstPythonProperty ( node , declaringModule , declaringType , GetLoc ( node ) ) ;
457
461
_scope . SetInScope ( node . Name , existing ) ;
458
462
}
459
463
@@ -471,8 +475,8 @@ private IPythonFunctionOverload CreateFunctionOverload(NameLookupContext funcSco
471
475
. ToArray ( ) ;
472
476
473
477
var overload = new AstPythonFunctionOverload (
474
- parameters ,
475
- funcScope . GetLocOfName ( node , node . NameExpression ) ,
478
+ parameters ,
479
+ funcScope . GetLocOfName ( node , node . NameExpression ) ,
476
480
node . ReturnAnnotation ? . ToCodeString ( _ast ) ) ;
477
481
_functionWalkers . Add ( new AstAnalysisFunctionWalker ( funcScope , node , overload ) ) ;
478
482
@@ -485,14 +489,13 @@ private static string GetDoc(SuiteStatement node) {
485
489
return ce ? . Value as string ;
486
490
}
487
491
488
- private AstPythonType CreateType ( ClassDefinition node ) {
489
- if ( node == null ) {
490
- throw new ArgumentNullException ( nameof ( node ) ) ;
491
- }
492
- return CreateBuiltinTypes
493
- ? new AstPythonBuiltinType ( node . Name , _module , node . StartIndex , GetDoc ( node . Body as SuiteStatement ) , GetLoc ( node ) )
494
- : new AstPythonType ( node . Name , _module , node . StartIndex , GetDoc ( node . Body as SuiteStatement ) , GetLoc ( node ) ) ;
492
+ private AstPythonClass CreateClass ( ClassDefinition node ) {
493
+ node = node ?? throw new ArgumentNullException ( nameof ( node ) ) ;
494
+ return new AstPythonClass ( node , _module ,
495
+ GetDoc ( node . Body as SuiteStatement ) , GetLoc ( node ) ,
496
+ CreateBuiltinTypes ? BuiltinTypeId . Unknown : BuiltinTypeId . Type ) ; // built-ins set type later
495
497
}
498
+
496
499
private void CollectTopLevelDefinitions ( ) {
497
500
var s = ( _ast . Body as SuiteStatement ) . Statements . ToArray ( ) ;
498
501
@@ -501,7 +504,7 @@ private void CollectTopLevelDefinitions() {
501
504
}
502
505
503
506
foreach ( var node in s . OfType < ClassDefinition > ( ) ) {
504
- _members [ node . Name ] = CreateType ( node ) ;
507
+ _members [ node . Name ] = CreateClass ( node ) ;
505
508
}
506
509
507
510
foreach ( var node in s . OfType < AssignmentStatement > ( ) . Where ( n => n . Right is NameExpression ) ) {
@@ -516,10 +519,12 @@ private void CollectTopLevelDefinitions() {
516
519
517
520
public override bool Walk ( ClassDefinition node ) {
518
521
var member = _scope . GetInScope ( node . Name ) ;
519
- var t = member as AstPythonType ??
520
- ( member as IPythonMultipleMembers ) ? . Members . OfType < AstPythonType > ( ) . FirstOrDefault ( pt => pt . StartIndex == node . StartIndex ) ;
522
+ var t = member as AstPythonClass ;
523
+ if ( t == null && member is IPythonMultipleMembers mm ) {
524
+ t = mm . GetMembers ( ) . OfType < AstPythonClass > ( ) . FirstOrDefault ( pt => pt . ClassDefinition . StartIndex == node . StartIndex ) ;
525
+ }
521
526
if ( t == null ) {
522
- t = CreateType ( node ) ;
527
+ t = CreateClass ( node ) ;
523
528
_scope . SetInScope ( node . Name , t ) ;
524
529
}
525
530
@@ -549,7 +554,7 @@ public void ProcessFunctionDefinition(FunctionDefinition node) {
549
554
var existing = _scope . LookupNameInScopes ( node . Name , NameLookupContext . LookupOptions . Local ) as AstPythonFunction ;
550
555
if ( existing == null ) {
551
556
var cls = _scope . GetInScope ( "__class__" ) as IPythonType ;
552
- existing = new AstPythonFunction ( _ast , _module , cls , node , GetLoc ( node ) ) ;
557
+ existing = new AstPythonFunction ( node , _module , cls , GetLoc ( node ) ) ;
553
558
_scope . SetInScope ( node . Name , existing ) ;
554
559
}
555
560
0 commit comments