@@ -169,10 +169,10 @@ _docstr(doc::DocStr, data) = (doc.data = merge(data, doc.data); doc)
169169macro ref (x)
170170 binding = bindingexpr (namify (x))
171171 typesig = signature (x)
172- esc (docexpr (binding, typesig))
172+ esc (docexpr (__source__, binding, typesig))
173173end
174174
175- docexpr (args... ) = Expr (:call , docstr, args... )
175+ docexpr (__source__, args... ) = Expr (:call , docstr, args... )
176176
177177function formatdoc (d:: DocStr )
178178 buffer = IOBuffer ()
@@ -482,26 +482,27 @@ isfield(x) = isexpr(x, :.) &&
482482# =========================
483483
484484"""
485- Docs.metadata(expr)
485+ Docs.metadata(source, expr)
486486
487487Build a `Dict` expression containing metadata captured from the expression `expr`.
488488
489489Fields that may be included in the returned `Dict`:
490490
491- - `:path`: String representing the file where `expr` is defined.
491+ - `:path`: Symbol representing the file where `expr` is defined.
492492- `:linenumber`: Linenumber where `expr` is defined.
493493- `:module`: Module where the docstring is defined.
494494- `:fields`: `Dict` of all field docs found in `expr`. Only for concrete types.
495495"""
496- function metadata (expr)
496+ function metadata (__source__, expr)
497497 args = []
498498 # Filename and linenumber of the docstring.
499- push! (args, :($ (Pair)(:path , $ (Base). @__FILE__ )))
500- push! (args, :($ (Pair)(:linenumber , $ (unsafe_load (cglobal (:jl_lineno , Cint))))))
499+ __file__ = isa (__source__. file, Symbol) ? String (__source__. file) : " "
500+ push! (args, Pair (:path , __file__))
501+ push! (args, Pair (:linenumber , __source__. line))
501502 # Module in which the docstring is defined.
502503 push! (args, :($ (Pair)(:module , $ (current_module)())))
503- # Field docs for concrete types.
504504 if isexpr (expr, :type )
505+ # Field docs for concrete types.
505506 fields = []
506507 tmp = nothing
507508 for each in expr. args[3 ]. args
@@ -518,37 +519,36 @@ function metadata(expr)
518519 :($ (Dict)($ (args... )))
519520end
520521
521- function keyworddoc (str, def)
522- docstr = esc (docexpr (lazy_iterpolate (str), metadata (def)))
523- :($ (keywords)[$ (esc (quot (def. name)))] = $ docstr)
522+ function keyworddoc (__source__, str, def)
523+ docstr = esc (docexpr (__source__, lazy_iterpolate (str), metadata (__source__, def)))
524+ return :($ (keywords)[$ (esc (quot (def. name)))] = $ docstr)
524525end
525526
526- function objectdoc (str, def, expr, sig = :(Union{}))
527+ function objectdoc (__source__, str, def, expr, sig = :(Union{}))
527528 binding = esc (bindingexpr (namify (expr)))
528- docstr = esc (docexpr (lazy_iterpolate (str), metadata (expr)))
529+ docstr = esc (docexpr (__source__, lazy_iterpolate (str), metadata (__source__, expr)))
529530 quote
530531 $ (esc (def))
531532 $ (doc!)($ binding, $ docstr, $ (esc (sig)))
532533 end
533534end
534535
535- function calldoc (str, def)
536+ function calldoc (__source__, str, def)
536537 args = def. args[2 : end ]
537538 if isempty (args) || all (validcall, args)
538- objectdoc (str, nothing , def, signature (def))
539+ objectdoc (__source__, str, nothing , def, signature (def))
539540 else
540541 docerror (def)
541542 end
542543end
543544validcall (x) = isa (x, Symbol) || isexpr (x, (:(:: ), :... , :kw , :parameters ))
544545
545- function moduledoc (meta, def, def′)
546+ function moduledoc (__source__, meta, def, def′)
546547 name = namify (def′)
547548 docex = Expr (:call , doc!, bindingexpr (name),
548- docexpr (lazy_iterpolate (meta), metadata (name))
549- )
549+ docexpr (__source__, lazy_iterpolate (meta), metadata (__source__, name)))
550550 if def === nothing
551- esc (:(eval ($ name, $ (quot (docex)))))
551+ esc (:($ eval ($ name, $ (quot (docex)))))
552552 else
553553 def = unblock (def)
554554 block = def. args[3 ]. args
@@ -562,18 +562,18 @@ function moduledoc(meta, def, def′)
562562end
563563
564564# Shares a single doc, `meta`, between several expressions from the tuple expression `ex`.
565- function multidoc (meta, ex, define)
565+ function multidoc (__source__, meta, ex, define)
566566 out = Expr (:toplevel )
567- str = docexpr (lazy_iterpolate (meta), metadata (ex))
567+ str = docexpr (__source__, lazy_iterpolate (meta), metadata (__source__, ex))
568568 ref = Ref {DocStr} ()
569569 for (n, arg) in enumerate (ex. args)
570570 # The first `arg` to be documented needs to also create the docstring for the group.
571571 # Subsequent `arg`s just need `ref` to be able to find the docstring without having
572572 # to create an entirely new one each.
573573 docstr = n === 1 ? :($ (ref)[] = $ str) : :($ (ref)[])
574- push! (out. args, :( @doc ( $ docstr, $ arg, $ define) ))
574+ push! (out. args, docm (__source__, docstr, arg, define))
575575 end
576- esc ( out)
576+ return out
577577end
578578
579579"""
@@ -649,7 +649,7 @@ isquotedmacrocall(x) =
649649isbasicdoc (x) = isexpr (x, :.) || isa (x, Union{QuoteNode, Symbol})
650650is_signature (x) = isexpr (x, :call ) || (isexpr (x, :(:: ), 2 ) && isexpr (x. args[1 ], :call )) || isexpr (x, :where )
651651
652- function docm (meta, ex, define = true )
652+ function docm (source :: LineNumberNode , meta, ex, define = true )
653653 # Some documented expressions may be decorated with macro calls which obscure the actual
654654 # expression. Expand the macro calls and remove extra blocks.
655655 x = unblock (macroexpand (ex))
@@ -665,7 +665,7 @@ function docm(meta, ex, define = true)
665665 # "..."
666666 # kw"if", kw"else"
667667 #
668- isa (x, Base. BaseDocs. Keyword) ? keyworddoc (meta, x) :
668+ isa (x, Base. BaseDocs. Keyword) ? keyworddoc (source, meta, x) :
669669
670670 # Method / macro definitions and "call" syntax.
671671 #
@@ -675,38 +675,38 @@ function docm(meta, ex, define = true)
675675 # function f end
676676 # f(...)
677677 #
678- isexpr (x, FUNC_HEADS) && is_signature (x. args[1 ]) ? objectdoc (meta, def, x, signature (x)) :
679- isexpr (x, :function ) && ! isexpr (x. args[1 ], :call ) ? objectdoc (meta, def, x) :
680- isexpr (x, :call ) ? calldoc (meta, x) :
678+ isexpr (x, FUNC_HEADS) && is_signature (x. args[1 ]) ? objectdoc (source, meta, def, x, signature (x)) :
679+ isexpr (x, :function ) && ! isexpr (x. args[1 ], :call ) ? objectdoc (source, meta, def, x) :
680+ isexpr (x, :call ) ? calldoc (source, meta, x) :
681681
682682 # Type definitions.
683683 #
684684 # type T ... end
685685 # abstract T
686686 # bitstype N T
687687 #
688- isexpr (x, [:type , :abstract , :bitstype ]) ? objectdoc (meta, def, x) :
688+ isexpr (x, [:type , :abstract , :bitstype ]) ? objectdoc (source, meta, def, x) :
689689
690690 # "Bindings". Names that resolve to objects with different names, ie.
691691 #
692692 # const T = S
693693 # T = S
694694 # global T = S
695695 #
696- isexpr (x, BINDING_HEADS) && ! isexpr (x. args[1 ], :call ) ? objectdoc (meta, def, x) :
696+ isexpr (x, BINDING_HEADS) && ! isexpr (x. args[1 ], :call ) ? objectdoc (source, meta, def, x) :
697697
698698 # Quoted macrocall syntax. `:@time` / `:(Base.@time)`.
699- isquotedmacrocall (x) ? objectdoc (meta, def, x) :
699+ isquotedmacrocall (x) ? objectdoc (source, meta, def, x) :
700700 # Modules and baremodules.
701- isexpr (x, :module ) ? moduledoc (meta, def, x) :
701+ isexpr (x, :module ) ? moduledoc (source, meta, def, x) :
702702 # Document several expressions with the same docstring. `a, b, c`.
703- isexpr (x, :tuple ) ? multidoc (meta, x, define) :
703+ isexpr (x, :tuple ) ? multidoc (source, meta, x, define) :
704704 # Errors generated by calling `macroexpand` are passed back to the call site.
705705 isexpr (x, :error ) ? esc (x) :
706706 # When documenting macro-generated code we look for embedded `@__doc__` calls.
707707 __doc__! (meta, x, define) ? esc (x) :
708708 # Any "basic" expression such as a bare function or module name or numeric literal.
709- isbasicdoc (x) ? objectdoc (meta, nothing , x) :
709+ isbasicdoc (x) ? objectdoc (source, meta, nothing , x) :
710710
711711 # All other expressions are undocumentable and should be handled on a case-by-case basis
712712 # with `@__doc__`. Unbound string literals are also undocumentable since they cannot be
@@ -725,9 +725,9 @@ function docerror(ex)
725725 :($ (error)($ txt, " \n " ))
726726end
727727
728- function docm (ex)
728+ function docm (source :: LineNumberNode , ex)
729729 if isexpr (ex, :-> )
730- docm (ex. args... )
730+ docm (source, ex. args... )
731731 elseif haskey (keywords, ex)
732732 parsedoc (keywords[ex])
733733 elseif isa (ex, Union{Expr, Symbol})
@@ -764,7 +764,7 @@ function loaddocs(docs)
764764 for (mod, ex, str, file, line) in docs
765765 data = Dict (:path => string (file), :linenumber => line)
766766 doc = docstr (str, data)
767- docstring = eval (mod, Expr (:body , Expr (:return , Expr (:call , QuoteNode (docm), QuoteNode (doc), QuoteNode (ex), false )))) # expand the real @doc macro now (using a hack because macroexpand takes current-module as an implicit argument)
767+ docstring = eval (mod, Expr (:body , Expr (:return , Expr (:call , QuoteNode (docm), QuoteNode (LineNumberNode (line, file)), QuoteNode ( doc), QuoteNode (ex), false )))) # expand the real @doc macro now (using a hack because macroexpand takes current-module as an implicit argument)
768768 eval (mod, Expr (:macrocall , unescape, nothing , docstring))
769769 end
770770 empty! (docs)
0 commit comments