@@ -112,7 +112,15 @@ public void Generate()
112112
113113 var compilationResult = Compilation . Create ( toCompile , new List < ISemanticAnalyzer > ( ) , Compilation . Settings . Default ) . Result ;
114114
115-
115+
116+ //compilationResult.Compilation.GetDiagnostics()
117+ // .Where(d => d.Severity == AX.Text.Diagnostics.DiagnosticSeverity.Error)
118+ // .ToList()
119+ // .ForEach(d =>
120+ // {
121+ // Log.Logger.Error(d.ToString());
122+ // });
123+
116124 this . CleanOutput ( this . OutputFolder ) ;
117125
118126 foreach ( var origin in projectSources )
@@ -194,7 +202,7 @@ public void Generate()
194202 }
195203
196204 //TargetProject.ProvisionProjectStructure();
197- GenerateMetadata ( compilationResult . Compilation ) ;
205+ GenerateMetadata ( compilationResult . Compilation , projectSources ) ;
198206 TargetProject . GenerateResources ( ) ;
199207 TargetProject . GenerateCompanionData ( ) ;
200208 Log . Logger . Information ( $ "Compilation of project '{ AxProject . SrcFolder } ' done.") ;
@@ -368,9 +376,18 @@ private string GetApaxFolderFromProjectReference(IReference projectReference)
368376 return string . Empty ;
369377 }
370378
371- private void GenerateMetadata ( Compilation compilation )
379+ private void GenerateMetadata ( Compilation compilation , IEnumerable < ( ISyntaxTree parseTree , SourceFileText source ) > sources )
372380 {
373- var meta = compilation . GetSemanticTree ( ) . Types . Where ( p => p . AccessModifier == AccessModifier . Public )
381+ var projectSourceFiles = sources . Select ( s => s . source . Filename ) . ToHashSet ( ) ;
382+
383+ var meta = compilation . GetSemanticTree ( ) . Types
384+ . Where ( p => true ) //p.AccessModifier == AccessModifier.Public | p.AccessModifier == AccessModifier.Internal)
385+ . Where ( p => {
386+ var typeLocation = p . Location ;
387+ if ( typeLocation == null ) return false ;
388+ var lineSpan = typeLocation . GetLineSpan ( ) ;
389+ return projectSourceFiles . Contains ( lineSpan . Filename ) ;
390+ } )
374391 . Select ( p => CreateMetaType ( p ) ) ;
375392
376393 using ( var swr = new StreamWriter ( Path . Combine ( EnsureFolder ( TargetProject . GetMetaDataFolder ) , "meta.json" ) ) )
@@ -417,13 +434,29 @@ private static string CreateMetaType(ITypeDeclaration type)
417434 sb . Append ( $ "TYPE { type . Name } : STRUCT ; END_STRUCT\n ") ;
418435 break ;
419436 case DeclarationKind . Class :
420- sb . Append ( $ "CLASS { type . Name } \n END_CLASS\n ") ;
437+ sb . Append ( $ "CLASS { type . Name } \n ") ;
438+ if ( type is IClassDeclaration classDecl )
439+ {
440+ foreach ( var method in classDecl . Methods )
441+ {
442+ sb . Append ( CreateMetaMethod ( method ) ) ;
443+ }
444+ }
445+ sb . Append ( "END_CLASS\n " ) ;
421446 break ;
422447 case DeclarationKind . Enumeration :
423448 sb . Append ( $ "TYPE { type . Name } : (item0); \n END_TYPE\n ") ;
424449 break ;
425450 case DeclarationKind . Interface :
426- sb . Append ( $ "INTERFACE { type . Name } \n END_INTERFACE\n ") ;
451+ sb . Append ( $ "INTERFACE { type . Name } \n ") ;
452+ if ( type is IInterfaceDeclaration interfaceDecl )
453+ {
454+ foreach ( var method in interfaceDecl . Methods )
455+ {
456+ sb . Append ( CreateMetaMethodPrototype ( method ) ) ;
457+ }
458+ }
459+ sb . Append ( "END_INTERFACE\n " ) ;
427460 break ;
428461 case DeclarationKind . NamedValueType :
429462 sb . Append ( $ "TYPE { type . Name } : INT (item0 := 0); \n END_TYPE\n ") ;
@@ -434,4 +467,64 @@ private static string CreateMetaType(ITypeDeclaration type)
434467
435468 return sb . ToString ( ) ;
436469 }
470+
471+ private static string CreateMetaMethod ( IMethodDeclaration method )
472+ {
473+ var sb = new StringBuilder ( ) ;
474+
475+ // Get return type
476+ var returnVariable = method . Variables . FirstOrDefault ( v => v . Section == Section . Return ) ;
477+ var returnType = returnVariable != null ? $ " : { returnVariable . Type . Name } " : string . Empty ;
478+
479+ // Build method signature with access modifier
480+ var accessModifier = method . AccessModifier . ToString ( ) . ToUpperInvariant ( ) ;
481+
482+ sb . Append ( $ "METHOD { accessModifier } { method . Name } { returnType } \n ") ;
483+
484+ // Add VAR_INPUT section if there are input parameters
485+ var inputVars = method . Variables . Where ( v => v . Section == Section . Input ) . ToList ( ) ;
486+ if ( inputVars . Any ( ) )
487+ {
488+ sb . Append ( "VAR_INPUT\n " ) ;
489+ foreach ( var inputVar in inputVars )
490+ {
491+ sb . Append ( $ "{ inputVar . Name } : { inputVar . Type . Name } ;\n ") ;
492+ }
493+ sb . Append ( "END_VAR\n " ) ;
494+ }
495+
496+ // Add at least one statement (semicolon) to method body
497+ sb . Append ( ";\n " ) ;
498+
499+ sb . Append ( "END_METHOD\n " ) ;
500+
501+ return sb . ToString ( ) ;
502+ }
503+
504+ private static string CreateMetaMethodPrototype ( IMethodPrototypeDeclaration method )
505+ {
506+ var sb = new StringBuilder ( ) ;
507+
508+ // Get return type
509+ var returnVariable = method . Variables . FirstOrDefault ( v => v . Section == Section . Return ) ;
510+ var returnType = returnVariable != null ? $ " : { returnVariable . Type . Name } " : string . Empty ;
511+
512+ sb . Append ( $ "METHOD { method . Name } { returnType } \n ") ;
513+
514+ // Add VAR_INPUT section if there are input parameters
515+ var inputVars = method . Variables . Where ( v => v . Section == Section . Input ) . ToList ( ) ;
516+ if ( inputVars . Any ( ) )
517+ {
518+ sb . Append ( "VAR_INPUT\n " ) ;
519+ foreach ( var inputVar in inputVars )
520+ {
521+ sb . Append ( $ "{ inputVar . Name } : { inputVar . Type . Name } ;\n ") ;
522+ }
523+ sb . Append ( "END_VAR\n " ) ;
524+ }
525+
526+ sb . Append ( "END_METHOD\n " ) ;
527+
528+ return sb . ToString ( ) ;
529+ }
437530}
0 commit comments