Changeset 1329

Show
Ignore:
Timestamp:
11/11/09 17:08:16 (2 years ago)
Author:
walter
Message:

initial safe function documentation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docsrc/function.dd

    r1327 r1329  
    5353            ) 
    5454{ 
    5555    x = i;    // error, modifying global state 
    5656    i = x;    // error, reading mutable global state 
    5757    i = y;    // ok, reading immutable global state 
    5858    i = *pz;  // error, reading const global state 
    5959    return i; 
    6060} 
    6161--- 
    6262 
     63    $(P Pure functions are covariant with impure ones.) 
     64 
    6365<h3>$(LNAME2 nothrow-functions, Nothrow Functions)</h3> 
    6466 
    6567    $(P Nothrow functions do not throw any exceptions derived 
    6668    from class $(I Exception). 
    6769    ) 
     70 
     71    $(P Nothrow functions are covariant with throwing ones.) 
    6872 
    6973<h3>$(LNAME2 ref-functions, Ref Functions)</h3> 
    7074 
    7175    $(P Ref functions allow functions to return by reference. 
    7276    This is analogous to ref function parameters. 
    7377    ) 
    7478 
    7579--- 
    7680ref int foo() 
    7781{   auto p = new int; 
    7882    return *p; 
    7983} 
    8084... 
    8185foo() = 3;  // reference returns can be lvalues 
     86--- 
     87 
     88<h3>$(LNAME2 property-functions, Property Functions)</h3> 
     89 
     90    $(P Property functions are tagged with the $(CODE @property) 
     91    attribute. They can be called without parentheses (hence 
     92    acting like properties). 
     93    ) 
     94 
     95--- 
     96struct S 
     97{ 
     98    int m_x; 
     99    @property 
     100    {   int x() { return m_x; } 
     101        int x(int newx) { return m_x = newx; } 
     102    } 
     103} 
     104 
     105void foo() 
     106{ 
     107    S s; 
     108    s.x = 3;   // calls s.x(int) 
     109    bar(s.x);  // calls bar(s.x()) 
     110} 
    82111--- 
    83112) 
    84113 
    85114<h3>$(LNAME2 virtual-functions, Virtual Functions)</h3> 
    86115 
    87116    $(P Virtual functions are functions that are called indirectly 
    88117    through a function 
    89118    pointer table, called a vtbl[], rather than directly. 
    90119    All non-static non-private non-template member functions are virtual. 
    91120    This may sound 
     
    15101539{ 
    15111540    return mixin(s); 
    15121541} 
    15131542 
    15141543const int x = foo("1"); 
    15151544--- 
    15161545 
    15171546    $(P is illegal, because the runtime code for foo() cannot be 
    15181547    generated. A function template would be the appropriate 
    15191548    method to implement this sort of thing.) 
     1549 
     1550$(V2 
     1551<h2>$(LNAME2 function-safety, Function Safety)</h2> 
     1552 
     1553    $(P $(I Safe functions) are functions that are statically checked 
     1554    to exhibit no possibility of 
     1555    $(LINK2 glossary.html#undefined_behavior, $(I undefined behavior)). 
     1556    Undefined behavior is often used as a vector for malicious 
     1557    attacks. 
     1558    ) 
     1559 
     1560<h3>$(LNAME2 safe-functions, Safe Functions)</h3> 
     1561 
     1562    $(P Safe functions are marked with the $(CODE @safe) attribute.) 
     1563 
     1564    $(P The following operations are not allowed in safe 
     1565    functions:) 
     1566 
     1567    $(UL 
     1568    $(LI No casting from a pointer type to any type other than $(CODE void*).) 
     1569    $(LI No casting from any non-pointer type to a pointer type.) 
     1570    $(LI No modification of pointer values.) 
     1571    $(LI Cannot access unions that have pointers or references overlapping 
     1572    with other types.) 
     1573    $(LI Calling any unsafe functions.) 
     1574    $(LI No catching of exceptions that are not derived from $(CODE class Exception).) 
     1575    $(LI No inline assembler.) 
     1576    $(LI No explicit casting of mutable objects to immutable.) 
     1577    $(LI No explicit casting of immutable objects to mutable.) 
     1578    $(LI No explicit casting of thread local objects to shared.) 
     1579    $(LI No explicit casting of shared objects to thread local.) 
     1580    $(LI No taking the address of a local variable or function parameter.) 
     1581    $(LI Cannot access $(D_KEYWORD __gshared) variables.) 
     1582    ) 
     1583 
     1584    $(P Functions nested inside safe functions default to being 
     1585    safe functions. 
     1586    ) 
     1587 
     1588    $(P Safe functions are covariant with trusted or unsafe functions.) 
     1589 
     1590    $(P $(B Note:) The verifiable safety of functions may be compromised by 
     1591    bugs in the compiler and specification. Please report all such errors 
     1592    so they can be corrected. 
     1593    ) 
     1594 
     1595<h3>$(LNAME2 trusted-functions, Trusted Functions)</h3> 
     1596 
     1597    $(P Trusted functions are marked with the $(CODE @trusted) attribute.) 
     1598 
     1599    $(P Trusted functions are guaranteed by the programmer to not exhibit 
     1600    any undefined behavior if called by a safe function. 
     1601    Generally, trusted functions should be kept small so that they are 
     1602    easier to manually verify. 
     1603    ) 
     1604 
     1605    $(P Trusted functions may call safe, trusted, or unsafe functions. 
     1606    ) 
     1607 
     1608    $(P Trusted functions are covariant with safe or unsafe functions.) 
     1609 
     1610<h3>$(LNAME2 unsafe-functions, Unsafe Functions)</h3> 
     1611 
     1612    $(P Unsafe functions are functions not marked with $(CODE @safe) or 
     1613    $(CODE @trusted) 
     1614    and are not nested inside $(CODE @safe) functions. 
     1615    A function being unsafe does not mean it actually is unsafe, it just 
     1616    means that the compiler is unable to verify that it cannot exhibit 
     1617    undefined behavior. 
     1618    ) 
     1619 
     1620    $(P Unsafe functions are $(B not) covariant with trusted or safe functions. 
     1621    ) 
     1622) 
     1623 
    15201624) 
    15211625 
    15221626Macros: 
    15231627    TITLE=Functions 
    15241628    WIKI=Function 
    15251629