statement
statement:
Typedef decl
Alias Decl
Decl
DECL:
Storage class DECL
Basic types of specifications;
Basic type declare function
Multiple declarations:
Statement initial value
Statement initial value, statement of statement initial value list
The initial value of the statement:
Declare
Disclaimer = initial value
List: List:
Fairman flag
List of declare flags, statement logo list
Significance flag:
Marker
Signifier = initial value
basic type:
Bit
Byte
Ubyte
Short
Ushort
int
Uint
Long
Ulong
charr
Wchar
Dchar
Float
Double
REAL
ifloat
iDouble
IREAL
cfloat
CDOUBLE
CREAL
Void
Signature list
Signature list
Typeof
TypeOf. Signature list
Basic Type 2:
*
[]
[Expression]
[ Types of ]
DELEGATE (parameter list)
Function (parameter list)
Declaration:
Basic type 2 declaration
Marker
(Statement)
Multiple declaration suffixes
(Statement) multiple declare suffixes
Multiple declared suffixes:
Refrigerator suffix
Declare suffix plurality of claim suffixes
Declare suffix:
[]
[Expression]
[ Types of ]
( parameter list )
Signature list:
Marker
Flag. Signature list
Template instance
Template instance. Signature list
TypeOf:
TypeOf
Storage category:
Abstract
Auto
Const
Deprecated
Final
Override
Static
SYNCHRONIZED
Types of:
basic type
Basic Type Declaring 2
Disclaimer 2:
Basic Type 2 Solution 2
(Declaring 2)
(Significance 2) Refrigerator suffix
parameter list:
parameter
Parameter, parameter list
...
parameter:
Declare
Fair = assignment expression
Inout statement
INOUT declaration = assignment expression
Inout:
in
OUT
inout
Declaration:
Typedef decl
Alias Decl
Decl
DECL:
StorageClass Decl
BasicType Declarators;
BasicType Declarator FunctionBody
Declarators:
Declaratorinitializer
DeclaratorInitializer, DeclaratorIdentifierList
DeclaratorInitializer:
Declarator
Declarator = Initializer
DeclaratorIdentifierList:
DeclaratorIdentifier
DeclaratorIdentifier, DeclaratorIdentifierList
DeclaratorIdentifier:
Identifier
Identifier = INITIALIZER
BasicType:
Bit
Byte
Ubyte
Short
Ushort
int
Uint
Long
Ulong
charr
Wchar
Dchar
Float
Double
REAL
ifloat
iDouble
IREAL
cfloat
CDOUBLE
CREAL
Void
.Identifierlist
IdentifierList
Typeof
Typeof. IdentifierList
Basictype2:
*
[]
[Expression]
[Type]
Delegate (parameterlist)
Function (parameterlist)
Declarator:
BasicType2 DeclaratorIdentifier
(Declarator)
Identifier Declaratorsuffixes
(Declarator) Declaratorsuffixes
Declaratorsuffixes:
Declaratorsuffix
Declaratorsuffix Declaratorsuffixes
Declaratorsuffix:
[]
[Expression]
[Type]
(ParameterList)
IdentifierList
Identifier
Identifier. IdentifierList
TemplateInstance
TemplateInstance. IdentifierList
Typeof
TypeOf (Expression)
StorageClass:
Abstract
Auto
Const
Deprecated
Final
Override
Static
SYNCHRONIZED
TYPE:
BasicType
BasicType Declarator2
Declarator2:
Basictype2 Declarator2
(Declarator2)
(Declarator2) Declaratorsuffxes
Parameterlist:
Parameter
Paremeter, parameterlist
...
Parameter:
Declarator
Declarator = AssignExpression
INOUT DECLARATOR
INOUT declarator = assignexpression
Inout:
in
OUT
inout
Statement grammar
Declaration syntax usually read from right to left:
INT X; // x is int
INT * X; // x is a pointer to INT
INT ** X; // x is a pointer to (pointing the INT "pointer)
Int [] x; // x is an INT array
INT * [] X; // x is an array of pointers to INT
int [] * x; // x is a pointer to the INT array
Array, read from left to right (column priority):
INT [3] x; // x is an int array with 3 elements
INT [3] [5] x; // x is an array of 5 elements, each element is an int array of 3 elements
INT [3] * [5] X; // x is an array of 5 elements, each element is a pointer to an array of INTs with 3 elements
Pointer to point to the function of the keyword, FUNCTION:
INT function (char) x; // x is a pointer to the function, the function has a char parameter, the return value type is int
INT function (char) [] x; // x is an array of pointers to the function, the function has a char parameter, the return value type is int
You can also use a C style array declaration method (line priority):
INT X [3]; // x is an int array with three elements
INT X [3] [5]; // x is an array of 3 elements, each element is an int array of 5 elements
INT (* x [5]) [3]; // x is an array of 5 elements, each element is a pointer to an array of INTs with 3 elements
INT (* x) (char); // x is a pointer to the function, the function has a char parameter, the return value type is int
INT (* [] x) (char); // x is the array of pointers that point to the function, the function has a char parameter, the return value type is Int in multiple declarations, all declared symbols have the same type:
Int x, y; // x and y are int
INT * x, y; // x and y is a pointer to int
INT X, * Y; / / error, multiple types
Int [] x, y; // x and y is int array
INT x [], y; // error, multiple types
Type definition
Strong type can be introduced through TypeDef. For function overload and debugger, in semantics, strong types are type check systems that can distinguish them with other types.
Typedef int myint;
Void foo (int x) {.}
Void foo (myint m) {.}
.
Myint B;
Foo (b); // Call foo (MyInt)
Typedef can specify a initial value different from its underlying type: Type:
Typedef int myint = 7;
Myint m; // initialized to 7
Type alias
Sometimes it is very convenient for type, for example, can be used as a shorter form of a lengthy complex type (such as a function pointer). In D, you can use an alias declaration to achieve this:
Alias abc.foo.bar myint;
The alias type is equivalent to the original type in semantics. The debugger cannot distinguish them, and the function overload will not distinguish them. E.g:
Alias Int Myint;
Void foo (int x) {.}
Void foo (Myint M) {.} // Error, multiple definitions of function foo
Type alias is equivalent to TypeDef in C.
Alias declaration
The symbol can be declared as an alias of another symbol. E.g:
IMPORT STRING;
Alias String.strlen Mylen;
...
INT LEN = Mylen ("Hello"); / / actually call String.Strlen ()
The following alias declaration is legal:
Template foo2 (t) {alias t t;}
Alias foo2! (int) T1;
Alias foo2! (int) .t t2;
Alias t1.t t3;
Alias T2 T4;
T1.t v1; // v1 type is int
T2 V2; // v2 type is int
T3 v3; // v3 type is int
T4 v4; // v4 type is int
The alias symbol can record a longer symbol as a shorter symbol, but also relocate a reference from one symbol to another symbol:
Version (Win32)
{
Alias Win32.foo myfoo;
}
Version (Linux)
{
Alias Linux.bar myfoo;
}
Alias can be used to import 'a symbol to the current scope from the module:
Alias string.strlen strlen;
Alias can also 'import' a series of overload functions so that you can overload functions in the current scope:
Class a {
INT foo (int a) {return 1;}
}
Class B: a {
INT foo (int A, uint b) {return 2;}
}
Class C: b {
INT foo (int a) {return 3;}
Alias B.foo foo;
}
Class D: c {
}
Void test ()
{
D b = new d (); int i;
I = B.foo (1, 2U); // Call B.foo
i = B.foo (1); // Call C.foo
}
Note: Type alias is sometimes very similar to the alias declaration:
Alias foo.bar abc; // Type or symbol?
In semantic analysis, these two situations will be distinguished.
Typeof
TypeOf is used to get a type of expression. E.g:
Void Func (INT i)
{
TypeOf (i) j; // j is int
TYPEOF (3 6.0) x; // x is Double
TypeOf (1) * p; // p is a pointer to the INT type
INT [typeof [p]] a; // a type is int [int *]
Printf ("% D / N", TypeOf ('c'). size); // Print 1
Double C = CAST (TypeOf (1.0)) j; // convert J to Double type
}
Expression will not be calculated, only the type of it will generate:
Void func ()
{INT i = 1;
TypeOf ( i) j; // j is declared as int, I will not increase
Printf ("% d / n", i); // Print 1
}
There are two special circumstances: Even in the member function, TypeOf (this) will also generate THIS in the type of non-static member function. Similarly, TypeOf (Super) will also generate the type of Super in the non-static member function.
Class a {}
Class B: a
{
TypeOf (this) x; // x is declared as B
TypeOf (Super) Y; // Y is declared as a type
}
Struct C
{
TypeOf (this) z; // Z is declared as a C * type
TypeOf (super) q; // error, c no parent structure
}
TypeOf (this) R; / / error, not included in the class or structure
TypeOf is very useful when writing template code.