# Copyright (c) Meta Platforms, Inc. and affiliates. schema codemarkup.python.2 { import src import code.24 import code.python import codemarkup.types import python import python.branches # Resolving locations to entities predicate PythonResolveLocation: { location: codemarkup.types.Location, entity: code.python.Entity, } { { Name, File, { span = Span }, nothing }, { decl = Decl } } where python.DeclarationsByFile { File, Span, Decl }; python.DeclarationToName Decl -> (Name : string); # Finding entities' locations predicate PythonEntityLocation: { entity: code.python.Entity, location: codemarkup.types.Location, } { { decl = D }, { Name, File, { span = Span }, nothing } } where if ( { imp = ImportStmt } = D; ) then ( # for imports just return import location python.DeclarationLocation { declaration = D, file = File, span = Span }; { from_name = python.Name Name } = ImportStmt; ) else if ( python.DeclarationDefinition {D, _}; ) then ( # if an entity has definition, just return its location # this should handle imports overriden with locally definded entities python.DeclarationLocation { declaration = D, file = File, span = Span }; python.DeclarationToName D -> (python.Name Name); ) else ( # for others trying to resolve original name to handle indirect imports python.DeclarationToName D -> SrcName; python.ResolveOriginalName { SrcName, OriginalName }; python.DeclarationWithName { OriginalName, OriginalDecl }; python.DeclarationLocation { declaration = OriginalDecl, file = File, span = Span }; OriginalName = python.Name Name; ) predicate PythonFileEntityXRefLocations: { file: src.File, xref: codemarkup.types.XRefLocation, entity: code.python.Entity, } {File, {Location , { span = Src } }, { decl = Decl }} where if (python.branches.FileToRoot { file = File }) then ( python.branches.DirectXRefByFile { File, {Decl, DeclFile, DeclSpan}, Src }; python.DeclarationToName Decl -> (python.Name Name); codemarkup.types.Location { Name, DeclFile, { span = DeclSpan }, nothing } = Location; ) else ( python.DirectXRefsByFile {File, { Decl, Src }}; PythonEntityLocation {{ decl = Decl }, Location}; ) # # Language entity uses # predicate PythonEntityUses: { target: code.python.Entity, file: src.File, span: src.ByteSpan, } { { decl = D }, File, Span } where python.DeclarationUses { declaration = D, file = File, span = Span} # # Language-specific symbol info # predicate NonImportPythonDeclarationKind: { declaration: python.Declaration, kind: codemarkup.types.SymbolKind, } { D, Kind } where ({cls = _} = D; Class_ = Kind ) | ({module = _} = D; Module = Kind ) | ({func = _} = D; python.ContainingTopLevelDeclaration { D, C }; ({module = _} = C; Function = Kind ) | ({cls = _} = C; Method = Kind ) | ({func = _} = C; Function = Kind )) | # these are locals ({variable = _} = D; python.ContainingTopLevelDeclaration { D, C }; ({module = _} = C; Variable = Kind ) | ({cls = _} = C; Field = Kind ) | ({func = _} = C; Variable = Kind )) # these are locals # For backward compatibility: predicate NonImportPythonDeclarationInfo: { declaration: python.Declaration, info: codemarkup.types.SymbolInfo, } { D, { kind = K, isAbstract = IsAbstract }} where IsAbstract = if (python.IsAbstract D) then (true) else (false); NonImportPythonDeclarationKind { D, K } predicate PythonEntityKind: { entity: code.python.Entity, kind: codemarkup.types.SymbolKind, } { { decl = D }, Kind } where NonImportPythonDeclarationKind { D, Kind } | ({imp = {from_name = FN}} = D; python.DeclarationWithName { FN, Imported }; NonImportPythonDeclarationKind { Imported, Kind } ) # # Annotations # predicate PythonAnnotation : { entity: code.python.Entity, anns: code.python.Annotations, } { { decl = Decl }, { decorators = Decorators }} where ( { cls = Cls } = Decl; python.ClassDefinition { declaration = Cls, decorators = { just = Decorators }}; ) | ( { func = Func } = Decl; python.FunctionDefinition { declaration = Func, decorators = { just = Decorators }}; ) # Just the visibility attributes # A single underscore indicates the intent that the symbol should be protected # A double underscore indicates the intent that the symbol should be private predicate PythonVisibility : { entity: code.python.Entity, visibility: codemarkup.types.Visibility, } { { decl = Decl }, Visibility } where python.DeclarationToName Decl -> Name; python.NameToSName Name -> { local_name = python.Name NameStr }; ( if ( NameStr = "__".. ) then ( Private ) else if ( NameStr = "_".. ) then ( Protected ) else ( Public ) ) = Visibility; # We expect to have at least static, async predicate PythonModifiers: { entity: code.python.Entity, modifiers: codemarkup.types.Modifiers } { { decl = Decl }, Modifiers } where ( { func = D } = Decl; python.FunctionDefinition { declaration = D, is_async = Async, decorators = MaybeDecorators }; IsStatic = if ( { just = Decorators } = MaybeDecorators; Decorators[..] = "@staticmethod" | "@classmethod"; ) then (true) else (false); ) | ( { variable = D } = Decl; IsStatic = if ( python.VariableDefinition { declaration = D, typeInfo = { just = { xrefs = Xrefs } } }; { target = "typing.ClassVar"} = Xrefs[..]; ) then (true) else (false); Async = false; ) | ( { cls = _ } = Decl; IsStatic = false; Async = false; ); IsAbstract = if (python.IsAbstract Decl) then (true) else (false); { isAbstract = IsAbstract, isFinal = false, isAsync = Async, isStatic = IsStatic, isReadonly = false, isConst = false, isMutable = false, isVolatile = false, isVirtual = false, isInline = false } = Modifiers; # For backward compatibility: predicate PythonEntityInfo: { entity: code.python.Entity, info: codemarkup.types.SymbolInfo, } { E, { kind = K, isAbstract = IsAbstract }} where { decl = Decl } = E; IsAbstract = if (python.IsAbstract Decl) then (true) else (false); PythonEntityKind { entity = E, kind = K } # All cases where we have a docstring span we also have a pretty docstring # span, so we can rely on python.DeclarationDocstring entirely for comments predicate PythonEntityComments: { entity : code.python.Entity, file : src.File, span : src.ByteSpan, text : maybe string } { { decl = Decl }, File, Span, { just = Text } } where python.DeclarationDocstring { Decl, Span, Text }; # should be second as not all decls have text python.DeclarationLocation { declaration = Decl, file = File } # # Relations # # Parent contains child by scope predicate PythonContainsChildEntity : { parent: code.python.Entity, child: code.python.Entity } { { decl = Container }, { decl = Member } } where !({ func = _ } = Container); # don't show content of functions python.Contains { Container, Member }; # Child is contained in scope of parent predicate PythonContainsParentEntity : { child: code.python.Entity, parent: code.python.Entity } { { decl = Child }, { decl = Parent } } where python.ContainedBy { Child, Parent }; # Child extends parent by an inheritance relationship predicate PythonExtendsParentEntity : { child : code.python.Entity, parent : code.python.Entity } { Child, Parent } where { decl = { cls = ChildClassDecl } } = Child; python.DerivedClassToBase { ChildClassDecl, ParentClassDecl }; { decl = { cls = ParentClassDecl } } = Parent; predicate PythonExtendsChildEntity : { parent: code.python.Entity, child: code.python.Entity } { Parent, Child } where { decl = {cls = ParentClassDecl} } = Parent; python.BaseClassToDerived { base = ParentClassDecl, derived = ChildClassDecl }; { decl = { cls = ChildClassDecl } } = Child; predicate PythonEntityModuleName : { entity: code.python.Entity, name: string } { { decl = { module = { name = python.Name Name } } }, Name } # # file call processing # predicate PythonFileCall : { file: src.File, callee_span: src.ByteSpan, call_args: [code.CallArgument], } {File, Span, CodeArgs} where ( python.FileCall { File, Span, PythonArgs}; ConvertCallArguments { PythonArgs, CodeArgs}; ) # (internal) helper predicate for CallArgument # Manually unrolled to lists of <=6 arguments predicate ConvertCallArguments : { pythonCallArguments: [python.CallArgument], codeCallArguments: [code.CallArgument] } {PythonArgs, CodeArgs} where ( ( [] = PythonArgs; [] = CodeArgs ) | ( [python.CallArgument{Label, Span, Argument}] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertLabel{Label, CodeLabel}; CodeArgs = [code.CallArgument{CodeLabel, Span, CodeArgument}] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6}, python.CallArgument{Label7, Span7, Argument7} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertArgument{Argument7, CodeArgument7}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; ConvertLabel{Label7, CodeLabel7}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6}, code.CallArgument{CodeLabel7, Span7, CodeArgument7} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6}, python.CallArgument{Label7, Span7, Argument7}, python.CallArgument{Label8, Span8, Argument8} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertArgument{Argument7, CodeArgument7}; ConvertArgument{Argument8, CodeArgument8}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; ConvertLabel{Label7, CodeLabel7}; ConvertLabel{Label8, CodeLabel8}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6}, code.CallArgument{CodeLabel7, Span7, CodeArgument7}, code.CallArgument{CodeLabel8, Span8, CodeArgument8} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6}, python.CallArgument{Label7, Span7, Argument7}, python.CallArgument{Label8, Span8, Argument8}, python.CallArgument{Label9, Span9, Argument9} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertArgument{Argument7, CodeArgument7}; ConvertArgument{Argument8, CodeArgument8}; ConvertArgument{Argument9, CodeArgument9}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; ConvertLabel{Label7, CodeLabel7}; ConvertLabel{Label8, CodeLabel8}; ConvertLabel{Label9, CodeLabel9}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6}, code.CallArgument{CodeLabel7, Span7, CodeArgument7}, code.CallArgument{CodeLabel8, Span8, CodeArgument8}, code.CallArgument{CodeLabel9, Span9, CodeArgument9} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6}, python.CallArgument{Label7, Span7, Argument7}, python.CallArgument{Label8, Span8, Argument8}, python.CallArgument{Label9, Span9, Argument9}, python.CallArgument{Label10, Span10, Argument10} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertArgument{Argument7, CodeArgument7}; ConvertArgument{Argument8, CodeArgument8}; ConvertArgument{Argument9, CodeArgument9}; ConvertArgument{Argument10, CodeArgument10}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; ConvertLabel{Label7, CodeLabel7}; ConvertLabel{Label8, CodeLabel8}; ConvertLabel{Label9, CodeLabel9}; ConvertLabel{Label10, CodeLabel10}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6}, code.CallArgument{CodeLabel7, Span7, CodeArgument7}, code.CallArgument{CodeLabel8, Span8, CodeArgument8}, code.CallArgument{CodeLabel9, Span9, CodeArgument9}, code.CallArgument{CodeLabel10, Span10, CodeArgument10} ] ) | ( [ python.CallArgument{Label, Span, Argument}, python.CallArgument{Label2, Span2, Argument2}, python.CallArgument{Label3, Span3, Argument3}, python.CallArgument{Label4, Span4, Argument4}, python.CallArgument{Label5, Span5, Argument5}, python.CallArgument{Label6, Span6, Argument6}, python.CallArgument{Label7, Span7, Argument7}, python.CallArgument{Label8, Span8, Argument8}, python.CallArgument{Label9, Span9, Argument9}, python.CallArgument{Label10, Span10, Argument10}, python.CallArgument{Label11, Span11, Argument11} ] = PythonArgs; ConvertArgument{Argument, CodeArgument}; ConvertArgument{Argument2, CodeArgument2}; ConvertArgument{Argument3, CodeArgument3}; ConvertArgument{Argument4, CodeArgument4}; ConvertArgument{Argument5, CodeArgument5}; ConvertArgument{Argument6, CodeArgument6}; ConvertArgument{Argument7, CodeArgument7}; ConvertArgument{Argument8, CodeArgument8}; ConvertArgument{Argument9, CodeArgument9}; ConvertArgument{Argument10, CodeArgument10}; ConvertArgument{Argument11, CodeArgument11}; ConvertLabel{Label, CodeLabel}; ConvertLabel{Label2, CodeLabel2}; ConvertLabel{Label3, CodeLabel3}; ConvertLabel{Label4, CodeLabel4}; ConvertLabel{Label5, CodeLabel5}; ConvertLabel{Label6, CodeLabel6}; ConvertLabel{Label7, CodeLabel7}; ConvertLabel{Label8, CodeLabel8}; ConvertLabel{Label9, CodeLabel9}; ConvertLabel{Label10, CodeLabel10}; ConvertLabel{Label11, CodeLabel11}; CodeArgs = [ code.CallArgument{CodeLabel, Span, CodeArgument}, code.CallArgument{CodeLabel2, Span2, CodeArgument2}, code.CallArgument{CodeLabel3, Span3, CodeArgument3}, code.CallArgument{CodeLabel4, Span4, CodeArgument4}, code.CallArgument{CodeLabel5, Span5, CodeArgument5}, code.CallArgument{CodeLabel6, Span6, CodeArgument6}, code.CallArgument{CodeLabel7, Span7, CodeArgument7}, code.CallArgument{CodeLabel8, Span8, CodeArgument8}, code.CallArgument{CodeLabel9, Span9, CodeArgument9}, code.CallArgument{CodeLabel10, Span10, CodeArgument10}, code.CallArgument{CodeLabel11, Span11, CodeArgument11} ] ) ) predicate ConvertArgument : { pythonArgument : maybe python.Argument , codeArgument : maybe code.Argument } {Python, Code} where ( { just = { lit = python.StringLiteral L } } = Python; { just = { lit = L } } = Code; ) | ( nothing = Python ; nothing = Code; ) predicate ConvertLabel : { python : maybe python.Name , code : maybe string } {Python, Code} where ( nothing = Python; nothing = Code) | (Python = {just = python.Name N}; Code = {just = N}) }