Free Pascal Language Reference Guide
Total Page:16
File Type:pdf, Size:1020Kb
Free Pascal : Reference guide. Reference guide for Free Pascal, version 2.4.2 Document version 2.4 November 2010 Michaël Van Canneyt Contents 1 Pascal Tokens 10 1.1 Symbols......................................... 10 1.2 Comments........................................ 11 1.3 Reserved words..................................... 12 1.3.1 Turbo Pascal reserved words.......................... 12 1.3.2 Free Pascal reserved words........................... 13 1.3.3 Object Pascal reserved words......................... 13 1.3.4 Modifiers.................................... 13 1.4 Identifiers........................................ 13 1.5 Hint directives...................................... 14 1.6 Numbers......................................... 15 1.7 Labels.......................................... 16 1.8 Character strings.................................... 17 2 Constants 19 2.1 Ordinary constants................................... 19 2.2 Typed constants..................................... 20 2.3 Resource strings..................................... 20 3 Types 22 3.1 Base types........................................ 22 3.1.1 Ordinal types.................................. 23 Integers..................................... 23 Boolean types.................................. 24 Enumeration types............................... 25 Subrange types................................. 26 3.1.2 Real types.................................... 27 3.2 Character types..................................... 27 3.2.1 Char....................................... 27 3.2.2 Strings..................................... 28 3.2.3 Short strings.................................. 28 1 CONTENTS 3.2.4 Ansistrings................................... 29 3.2.5 UnicodeStrings................................. 31 3.2.6 WideStrings................................... 31 3.2.7 Constant strings................................. 31 3.2.8 PChar - Null terminated strings........................ 32 3.3 Structured Types.................................... 32 Packed structured types............................. 33 3.3.1 Arrays...................................... 34 Static arrays................................... 34 Dynamic arrays................................. 36 Packing and unpacking an array........................ 38 3.3.2 Record types.................................. 38 3.3.3 Set types.................................... 42 3.3.4 File types.................................... 43 3.4 Pointers......................................... 43 3.5 Forward type declarations................................ 45 3.6 Procedural types..................................... 46 3.7 Variant types...................................... 47 3.7.1 Definition.................................... 47 3.7.2 Variants in assignments and expressions.................... 48 3.7.3 Variants and interfaces............................. 49 4 Variables 51 4.1 Definition........................................ 51 4.2 Declaration....................................... 51 4.3 Scope.......................................... 53 4.4 Initialized variables................................... 53 4.5 Thread Variables.................................... 54 4.6 Properties........................................ 54 5 Objects 58 5.1 Declaration....................................... 58 5.2 Fields.......................................... 59 5.3 Static fields....................................... 60 5.4 Constructors and destructors.............................. 61 5.5 Methods......................................... 62 5.5.1 Declaration................................... 62 5.5.2 Method invocation............................... 63 Static methods................................. 63 Virtual methods................................. 64 Abstract methods................................ 65 2 CONTENTS 5.6 Visibility........................................ 66 6 Classes 67 6.1 Class definitions..................................... 67 6.2 Class instantiation.................................... 71 6.3 Methods......................................... 71 6.3.1 Declaration................................... 71 6.3.2 invocation.................................... 71 6.3.3 Virtual methods................................. 72 6.3.4 Class methods................................. 73 6.3.5 Message methods................................ 73 6.3.6 Using inherited................................. 75 6.4 Properties........................................ 76 6.4.1 Definition.................................... 76 6.4.2 Indexed properties............................... 78 6.4.3 Array properties................................ 79 6.4.4 Default properties................................ 80 6.4.5 Storage information.............................. 80 6.4.6 Overriding properties.............................. 81 7 Interfaces 83 7.1 Definition........................................ 83 7.2 Interface identification: A GUID............................ 84 7.3 Interface implementations............................... 85 7.4 Interfaces and COM................................... 86 7.5 CORBA and other Interfaces.............................. 86 7.6 Reference counting................................... 86 8 Generics 88 8.1 Introduction....................................... 88 8.2 Generic class definition................................. 88 8.3 Generic class specialization............................... 90 8.4 A word about scope................................... 91 9 Expressions 94 9.1 Expression syntax.................................... 95 9.2 Function calls...................................... 96 9.3 Set constructors..................................... 98 9.4 Value typecasts..................................... 98 9.5 Variable typecasts.................................... 99 9.6 Unaligned typecasts................................... 100 3 CONTENTS 9.7 The @ operator..................................... 100 9.8 Operators........................................ 101 9.8.1 Arithmetic operators.............................. 101 9.8.2 Logical operators................................ 102 9.8.3 Boolean operators............................... 102 9.8.4 String operators................................. 103 9.8.5 Set operators.................................. 103 9.8.6 Relational operators.............................. 105 9.8.7 Class operators................................. 106 10 Statements 108 10.1 Simple statements.................................... 108 10.1.1 Assignments.................................. 108 10.1.2 Procedure statements.............................. 109 10.1.3 Goto statements................................. 110 10.2 Structured statements.................................. 111 10.2.1 Compound statements............................. 111 10.2.2 The Case statement.............................. 112 10.2.3 The If..then..else statement...................... 113 10.2.4 The For..to/downto..do statement................... 114 10.2.5 The For..in..do statement......................... 115 10.2.6 The Repeat..until statement....................... 122 10.2.7 The While..do statement.......................... 123 10.2.8 The With statement.............................. 124 10.2.9 Exception Statements.............................. 125 10.3 Assembler statements.................................. 125 11 Using functions and procedures 127 11.1 Procedure declaration.................................. 127 11.2 Function declaration.................................. 128 11.3 Function results..................................... 128 11.4 Parameter lists...................................... 129 11.4.1 Value parameters................................ 129 11.4.2 Variable parameters............................... 130 11.4.3 Out parameters................................. 131 11.4.4 Constant parameters.............................. 132 11.4.5 Open array parameters............................. 133 11.4.6 Array of const................................. 133 11.5 Function overloading.................................. 136 11.6 Forward defined functions............................... 136 11.7 External functions.................................... 137 4 CONTENTS 11.8 Assembler functions.................................. 138 11.9 Modifiers........................................ 138 11.9.1 alias....................................... 139 11.9.2 cdecl...................................... 140 11.9.3 export...................................... 141 11.9.4 inline...................................... 141 11.9.5 interrupt..................................... 141 11.9.6 iocheck..................................... 141 11.9.7 local....................................... 142 11.9.8 nostackframe.................................. 142 11.9.9 overload..................................... 142 11.9.10 pascal...................................... 143 11.9.11 public...................................... 143 11.9.12 register..................................... 144 11.9.13 safecall..................................... 144 11.9.14 saveregisters.................................. 144 11.9.15 softfloat..................................... 145 11.9.16 stdcall...................................... 145 11.9.17 varargs..................................... 145 11.10Unsupported Turbo Pascal modifiers.......................... 145 12 Operator overloading 146 12.1 Introduction....................................... 146 12.2 Operator declarations.................................. 146 12.3 Assignment operators.................................