pdecsub.pas 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. function check_proc_directive(isprocvar:boolean):boolean;
  49. function proc_get_importname(pd:tprocdef):string;
  50. procedure proc_set_mangledname(pd:tprocdef);
  51. procedure parse_parameter_dec(pd:tabstractprocdef);
  52. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  53. procedure parse_var_proc_directives(sym:tsym);
  54. procedure parse_object_proc_directives(pd:tabstractprocdef);
  55. procedure parse_record_proc_directives(pd:tabstractprocdef);
  56. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  57. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  58. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean;astruct:tabstractrecorddef);
  59. { parse a record method declaration (not a (class) constructor/destructor) }
  60. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  61. { helper functions - they insert nested objects hierarchy to the symtablestack
  62. with object hierarchy
  63. }
  64. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  65. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  66. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  67. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  68. implementation
  69. uses
  70. SysUtils,
  71. { common }
  72. cutils,
  73. { global }
  74. globtype,globals,verbose,constexp,
  75. systems,
  76. cpuinfo,
  77. { assembler }
  78. aasmbase,
  79. { symtable }
  80. symbase,symcpu,symtable,symutil,defutil,defcmp,
  81. { parameter handling }
  82. paramgr,cpupara,
  83. { pass 1 }
  84. fmodule,node,htypechk,ncon,nld,
  85. objcutil,
  86. { parser }
  87. scanner,
  88. syscinfo,
  89. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  90. {$ifdef jvm}
  91. ,pjvm
  92. {$endif}
  93. ;
  94. const
  95. { Please leave this here, this module should NOT use
  96. these variables.
  97. Declaring it as string here results in an error when compiling (PFV) }
  98. current_procinfo = 'error';
  99. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  100. var
  101. _class,hp : tobjectdef;
  102. begin
  103. if obj.typ=recorddef then
  104. begin
  105. symtablestack.push(obj.symtable);
  106. result:=1;
  107. exit;
  108. end;
  109. result:=0;
  110. { insert class hierarchy in the reverse order }
  111. hp:=nil;
  112. repeat
  113. _class:=tobjectdef(obj);
  114. while _class.childof<>hp do
  115. _class:=_class.childof;
  116. hp:=_class;
  117. symtablestack.push(_class.symtable);
  118. inc(result);
  119. until hp=obj;
  120. end;
  121. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  122. begin
  123. result:=0;
  124. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  125. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  126. inc(result,push_child_hierarchy(obj));
  127. end;
  128. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  129. var
  130. _class : tobjectdef;
  131. begin
  132. if obj.typ=recorddef then
  133. begin
  134. symtablestack.pop(obj.symtable);
  135. result:=1;
  136. exit;
  137. end;
  138. result:=0;
  139. _class:=tobjectdef(obj);
  140. while assigned(_class) do
  141. begin
  142. symtablestack.pop(_class.symtable);
  143. _class:=_class.childof;
  144. inc(result);
  145. end;
  146. end;
  147. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  148. begin
  149. result:=pop_child_hierarchy(obj);
  150. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  151. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  152. end;
  153. procedure check_msg_para(p:TObject;arg:pointer);
  154. begin
  155. if (tsym(p).typ<>paravarsym) then
  156. exit;
  157. with tparavarsym(p) do
  158. begin
  159. { Count parameters }
  160. if (paranr>=10) then
  161. inc(plongint(arg)^);
  162. { First parameter must be var }
  163. if (paranr=10) and
  164. (varspez<>vs_var) then
  165. MessagePos(fileinfo,parser_e_ill_msg_param);
  166. end;
  167. end;
  168. procedure parse_parameter_dec(pd:tabstractprocdef);
  169. {
  170. handle_procvar needs the same changes
  171. }
  172. type
  173. tppv = (pv_none,pv_proc,pv_func);
  174. var
  175. sc : TFPObjectList;
  176. hdef : tdef;
  177. arrayelementdef : tdef;
  178. vs : tparavarsym;
  179. i : longint;
  180. srsym : tsym;
  181. pv : tprocvardef;
  182. varspez : Tvarspez;
  183. defaultvalue : tconstsym;
  184. defaultrequired : boolean;
  185. old_block_type : tblock_type;
  186. currparast : tparasymtable;
  187. parseprocvar : tppv;
  188. locationstr : string;
  189. paranr : integer;
  190. dummytype : ttypesym;
  191. explicit_paraloc,
  192. need_array,
  193. is_univ: boolean;
  194. stoptions : TSingleTypeOptions;
  195. procedure handle_default_para_value;
  196. var
  197. convpd : tprocdef;
  198. doconv : tconverttype;
  199. nodetype : tnodetype;
  200. bt : tblock_type;
  201. begin
  202. { only allowed for types that can be represented by a
  203. constant expression }
  204. if try_to_consume(_EQ) then
  205. begin
  206. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  207. is_object(hdef) or
  208. ((hdef.typ=arraydef) and
  209. not is_dynamic_array(hdef)) then
  210. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  211. vs:=tparavarsym(sc[0]);
  212. if sc.count>1 then
  213. Message(parser_e_default_value_only_one_para);
  214. if not(vs.varspez in [vs_value,vs_const,vs_constref]) then
  215. Message(parser_e_default_value_val_const);
  216. bt:=block_type;
  217. block_type:=bt_const;
  218. { prefix 'def' to the parameter name }
  219. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  220. block_type:=bt;
  221. if assigned(defaultvalue) then
  222. begin
  223. include(defaultvalue.symoptions,sp_internal);
  224. pd.parast.insert(defaultvalue);
  225. { check whether the default value is of the correct
  226. type }
  227. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  228. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  229. end;
  230. defaultrequired:=true;
  231. end
  232. else
  233. begin
  234. if defaultrequired then
  235. Message1(parser_e_default_value_expected_for_para,vs.name);
  236. end;
  237. end;
  238. begin
  239. old_block_type:=block_type;
  240. explicit_paraloc:=false;
  241. consume(_LKLAMMER);
  242. { Delphi/Kylix supports nonsense like }
  243. { procedure p(); }
  244. if try_to_consume(_RKLAMMER) and
  245. not(m_tp7 in current_settings.modeswitches) then
  246. exit;
  247. { parsing a proc or procvar ? }
  248. currparast:=tparasymtable(pd.parast);
  249. { reset }
  250. sc:=TFPObjectList.create(false);
  251. defaultrequired:=false;
  252. paranr:=0;
  253. block_type:=bt_var;
  254. is_univ:=false;
  255. repeat
  256. parseprocvar:=pv_none;
  257. if try_to_consume(_VAR) then
  258. varspez:=vs_var
  259. else
  260. if try_to_consume(_CONST) then
  261. varspez:=vs_const
  262. else
  263. if (m_out in current_settings.modeswitches) and
  264. try_to_consume(_OUT) then
  265. varspez:=vs_out
  266. else
  267. if try_to_consume(_CONSTREF) then
  268. varspez:=vs_constref
  269. else
  270. if (m_mac in current_settings.modeswitches) and
  271. try_to_consume(_POINTPOINTPOINT) then
  272. begin
  273. include(pd.procoptions,po_varargs);
  274. break;
  275. end
  276. else
  277. if (m_nested_procvars in current_settings.modeswitches) and
  278. try_to_consume(_PROCEDURE) then
  279. begin
  280. parseprocvar:=pv_proc;
  281. varspez:=vs_const;
  282. end
  283. else
  284. if (m_nested_procvars in current_settings.modeswitches) and
  285. try_to_consume(_FUNCTION) then
  286. begin
  287. parseprocvar:=pv_func;
  288. varspez:=vs_const;
  289. end
  290. else
  291. varspez:=vs_value;
  292. defaultvalue:=nil;
  293. hdef:=nil;
  294. { read identifiers and insert with error type }
  295. sc.clear;
  296. repeat
  297. inc(paranr);
  298. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  299. currparast.insert(vs);
  300. if assigned(vs.owner) then
  301. sc.add(vs)
  302. else
  303. vs.free;
  304. consume(_ID);
  305. until not try_to_consume(_COMMA);
  306. locationstr:='';
  307. { macpas anonymous procvar }
  308. if parseprocvar<>pv_none then
  309. begin
  310. { inline procvar definitions are always nested procvars }
  311. pv:=cprocvardef.create(normal_function_level+1);
  312. if token=_LKLAMMER then
  313. parse_parameter_dec(pv);
  314. if parseprocvar=pv_func then
  315. begin
  316. block_type:=bt_var_type;
  317. consume(_COLON);
  318. single_type(pv.returndef,[]);
  319. block_type:=bt_var;
  320. end;
  321. hdef:=pv;
  322. { possible proc directives }
  323. if check_proc_directive(true) then
  324. begin
  325. dummytype:=ctypesym.create('unnamed',hdef);
  326. parse_var_proc_directives(tsym(dummytype));
  327. dummytype.typedef:=nil;
  328. hdef.typesym:=nil;
  329. dummytype.free;
  330. end;
  331. { Add implicit hidden parameters and function result }
  332. handle_calling_convention(pv,hcc_default_actions_intf);
  333. {$ifdef jvm}
  334. { anonymous -> no name }
  335. jvm_create_procvar_class('',pv);
  336. {$endif}
  337. end
  338. else
  339. { read type declaration, force reading for value paras }
  340. if (token=_COLON) or (varspez=vs_value) then
  341. begin
  342. consume(_COLON);
  343. { check for an open array }
  344. need_array:=false;
  345. { bitpacked open array are not yet supported }
  346. if (token=_PACKED) and
  347. not(cs_bitpacking in current_settings.localswitches) then
  348. begin
  349. consume(_PACKED);
  350. need_array:=true;
  351. end;
  352. if (token=_ARRAY) or
  353. need_array then
  354. begin
  355. consume(_ARRAY);
  356. consume(_OF);
  357. { define range and type of range }
  358. hdef:=carraydef.create_openarray;
  359. { array of const ? }
  360. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  361. begin
  362. consume(_CONST);
  363. srsym:=search_system_type('TVARREC');
  364. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  365. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  366. end
  367. else
  368. begin
  369. { define field type }
  370. if m_delphi in current_settings.modeswitches then
  371. stoptions:=[stoAllowSpecialization]
  372. else
  373. stoptions:=[];
  374. single_type(arrayelementdef,stoptions);
  375. tarraydef(hdef).elementdef:=arrayelementdef;
  376. end;
  377. end
  378. else
  379. begin
  380. if (m_mac in current_settings.modeswitches) then
  381. is_univ:=try_to_consume(_UNIV);
  382. if try_to_consume(_TYPE) then
  383. hdef:=ctypedformaltype
  384. else
  385. begin
  386. block_type:=bt_var_type;
  387. single_type(hdef,[stoAllowSpecialization]);
  388. block_type:=bt_var;
  389. end;
  390. { open string ? }
  391. if is_shortstring(hdef) then
  392. begin
  393. case varspez of
  394. vs_var,vs_out:
  395. begin
  396. { not 100% Delphi-compatible: type xstr=string[255] cannot
  397. become an openstring there, while here it can }
  398. if (cs_openstring in current_settings.moduleswitches) and
  399. (tstringdef(hdef).len=255) then
  400. hdef:=openshortstringtype
  401. end;
  402. vs_value:
  403. begin
  404. { value "openstring" parameters don't make sense (the
  405. original string can never be modified, so there's no
  406. use in passing its original length), so change these
  407. into regular shortstring parameters (seems to be what
  408. Delphi also does) }
  409. if is_open_string(hdef) then
  410. hdef:=cshortstringtype;
  411. end;
  412. end;
  413. end;
  414. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  415. begin
  416. if (idtoken=_LOCATION) then
  417. begin
  418. consume(_LOCATION);
  419. locationstr:=cstringpattern;
  420. consume(_CSTRING);
  421. end
  422. else
  423. begin
  424. if explicit_paraloc then
  425. Message(parser_e_paraloc_all_paras);
  426. locationstr:='';
  427. end;
  428. end
  429. else
  430. locationstr:='';
  431. { default parameter }
  432. if (m_default_para in current_settings.modeswitches) then
  433. handle_default_para_value;
  434. end;
  435. end
  436. else
  437. hdef:=cformaltype;
  438. { File types are only allowed for var and out parameters }
  439. if (hdef.typ=filedef) and
  440. not(varspez in [vs_out,vs_var]) then
  441. CGMessage(cg_e_file_must_call_by_reference);
  442. { Dispinterfaces are restricted to using only automatable types }
  443. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  444. not is_automatable(hdef) then
  445. Message1(type_e_not_automatable,hdef.typename);
  446. { univ cannot be used with types whose size is not known at compile
  447. time }
  448. if is_univ and
  449. not is_valid_univ_para_type(hdef) then
  450. Message1(parser_e_invalid_univ_para,hdef.typename);
  451. for i:=0 to sc.count-1 do
  452. begin
  453. vs:=tparavarsym(sc[i]);
  454. vs.univpara:=is_univ;
  455. { update varsym }
  456. vs.vardef:=hdef;
  457. vs.defaultconstsym:=defaultvalue;
  458. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  459. begin
  460. if locationstr<>'' then
  461. begin
  462. if sc.count>1 then
  463. Message(parser_e_paraloc_only_one_para);
  464. if (paranr>1) and not(explicit_paraloc) then
  465. Message(parser_e_paraloc_all_paras);
  466. explicit_paraloc:=true;
  467. include(vs.varoptions,vo_has_explicit_paraloc);
  468. if not(paramanager.parseparaloc(vs,locationstr)) then
  469. message(parser_e_illegal_explicit_paraloc);
  470. end
  471. else
  472. if explicit_paraloc then
  473. Message(parser_e_paraloc_all_paras);
  474. end;
  475. end;
  476. until not try_to_consume(_SEMICOLON);
  477. if explicit_paraloc then
  478. begin
  479. pd.has_paraloc_info:=callerside;
  480. include(pd.procoptions,po_explicitparaloc);
  481. end;
  482. { remove parasymtable from stack }
  483. sc.free;
  484. { reset object options }
  485. block_type:=old_block_type;
  486. consume(_RKLAMMER);
  487. end;
  488. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  489. var
  490. hs : string;
  491. orgsp,sp,orgspnongen,spnongen : TIDString;
  492. dummysym,srsym : tsym;
  493. checkstack : psymtablestackitem;
  494. oldfilepos,
  495. classstartfilepos,
  496. procstartfilepos : tfileposinfo;
  497. i,
  498. index : longint;
  499. addgendummy,
  500. hadspecialize,
  501. firstpart,
  502. found,
  503. searchagain : boolean;
  504. st,
  505. genericst: TSymtable;
  506. aprocsym : tprocsym;
  507. popclass : integer;
  508. ImplIntf : TImplementedInterface;
  509. old_parse_generic : boolean;
  510. old_current_structdef: tabstractrecorddef;
  511. old_current_genericdef,
  512. old_current_specializedef: tstoreddef;
  513. lasttoken,lastidtoken: ttoken;
  514. genericparams : tfphashobjectlist;
  515. procedure parse_operator_name;
  516. begin
  517. if (lasttoken in [first_overloaded..last_overloaded]) then
  518. begin
  519. optoken:=token;
  520. end
  521. else
  522. begin
  523. case lasttoken of
  524. _CARET:
  525. Message1(parser_e_overload_operator_failed,'**');
  526. _ID:
  527. case lastidtoken of
  528. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  529. _EXPLICIT:optoken:=_OP_EXPLICIT;
  530. _INC:optoken:=_OP_INC;
  531. _DEC:optoken:=_OP_DEC;
  532. _INITIALIZE:optoken:=_OP_INITIALIZE;
  533. _FINALIZE:optoken:=_OP_FINALIZE;
  534. _ADDREF:optoken:=_OP_ADDREF;
  535. _COPY:optoken:=_OP_COPY;
  536. else
  537. if (m_delphi in current_settings.modeswitches) then
  538. case lastidtoken of
  539. _IMPLICIT:optoken:=_ASSIGNMENT;
  540. _NEGATIVE:optoken:=_MINUS;
  541. _POSITIVE:optoken:=_PLUS;
  542. _LOGICALNOT:optoken:=_OP_NOT;
  543. _IN:optoken:=_OP_IN;
  544. _EQUAL:optoken:=_EQ;
  545. _NOTEQUAL:optoken:=_NE;
  546. _GREATERTHAN:optoken:=_GT;
  547. _GREATERTHANOREQUAL:optoken:=_GTE;
  548. _LESSTHAN:optoken:=_LT;
  549. _LESSTHANOREQUAL:optoken:=_LTE;
  550. _ADD:optoken:=_PLUS;
  551. _SUBTRACT:optoken:=_MINUS;
  552. _MULTIPLY:optoken:=_STAR;
  553. _DIVIDE:optoken:=_SLASH;
  554. _INTDIVIDE:optoken:=_OP_DIV;
  555. _MODULUS:optoken:=_OP_MOD;
  556. _LEFTSHIFT:optoken:=_OP_SHL;
  557. _RIGHTSHIFT:optoken:=_OP_SHR;
  558. _LOGICALAND:optoken:=_OP_AND;
  559. _LOGICALOR:optoken:=_OP_OR;
  560. _LOGICALXOR:optoken:=_OP_XOR;
  561. _BITWISEAND:optoken:=_OP_AND;
  562. _BITWISEOR:optoken:=_OP_OR;
  563. _BITWISEXOR:optoken:=_OP_XOR;
  564. else
  565. Message1(parser_e_overload_operator_failed,'');
  566. end
  567. else
  568. Message1(parser_e_overload_operator_failed,'');
  569. end
  570. else
  571. Message1(parser_e_overload_operator_failed,'');
  572. end;
  573. end;
  574. sp:=overloaded_names[optoken];
  575. orgsp:=sp;
  576. spnongen:=sp;
  577. orgspnongen:=orgsp;
  578. end;
  579. procedure consume_proc_name;
  580. var
  581. s : string;
  582. i : longint;
  583. sym : ttypesym;
  584. begin
  585. lasttoken:=token;
  586. lastidtoken:=idtoken;
  587. if assigned(genericparams) then
  588. for i:=0 to genericparams.count-1 do
  589. begin
  590. sym:=ttypesym(genericparams[i]);
  591. if tstoreddef(sym.typedef).is_registered then
  592. begin
  593. sym.typedef.free;
  594. sym.typedef:=nil;
  595. end;
  596. sym.free;
  597. end;
  598. genericparams.free;
  599. genericparams:=nil;
  600. hadspecialize:=false;
  601. if potype=potype_operator then
  602. optoken:=NOTOKEN;
  603. if (potype=potype_operator) and (token<>_ID) then
  604. begin
  605. parse_operator_name;
  606. consume(token);
  607. end
  608. else
  609. begin
  610. sp:=pattern;
  611. orgsp:=orgpattern;
  612. spnongen:=sp;
  613. orgspnongen:=orgsp;
  614. if firstpart and
  615. not (m_delphi in current_settings.modeswitches) and
  616. (idtoken=_SPECIALIZE) then
  617. hadspecialize:=true;
  618. consume(_ID);
  619. if (isgeneric or (m_delphi in current_settings.modeswitches)) and
  620. (token in [_LT,_LSHARPBRACKET]) then
  621. begin
  622. consume(token);
  623. if token in [_GT,_RSHARPBRACKET] then
  624. message(type_e_type_id_expected)
  625. else
  626. begin
  627. genericparams:=parse_generic_parameters(true);
  628. if not assigned(genericparams) then
  629. internalerror(2015061201);
  630. if genericparams.count=0 then
  631. internalerror(2015061202);
  632. s:='';
  633. str(genericparams.count,s);
  634. spnongen:=sp;
  635. orgspnongen:=orgsp;
  636. sp:=sp+'$'+s;
  637. orgsp:=orgsp+'$'+s;
  638. end;
  639. if not try_to_consume(_GT) then
  640. consume(_RSHARPBRACKET);
  641. end;
  642. end;
  643. firstpart:=false;
  644. end;
  645. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  646. var
  647. storepos:tfileposinfo;
  648. srsymtable:TSymtable;
  649. begin
  650. storepos:=current_tokenpos;
  651. current_tokenpos:=procstartfilepos;
  652. searchsym(sp,result,srsymtable);
  653. if not assigned(result) then
  654. begin
  655. if gen_error then
  656. identifier_not_found(orgsp);
  657. result:=generrorsym;
  658. end;
  659. current_tokenpos:=storepos;
  660. end;
  661. procedure consume_generic_interface;
  662. var
  663. genparalist : tfpobjectlist;
  664. prettyname,
  665. specializename : ansistring;
  666. genname,
  667. ugenname : tidstring;
  668. gencount : string;
  669. begin
  670. consume(_LSHARPBRACKET);
  671. genparalist:=tfpobjectlist.create(false);
  672. if not parse_generic_specialization_types(genparalist,nil,prettyname,specializename) then
  673. srsym:=generrorsym
  674. else
  675. begin
  676. str(genparalist.count,gencount);
  677. genname:=sp+'$'+gencount;
  678. { ToDo: handle nested interfaces }
  679. genname:=generate_generic_name(genname,specializename,'');
  680. ugenname:=upper(genname);
  681. srsym:=search_object_name(ugenname,false);
  682. if not assigned(srsym) then
  683. begin
  684. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  685. srsym:=nil;
  686. exit;
  687. end;
  688. end;
  689. genparalist.free;
  690. consume(_RSHARPBRACKET);
  691. end;
  692. function handle_generic_interface:boolean;
  693. var
  694. i : longint;
  695. sym : ttypesym;
  696. typesrsym : tsym;
  697. typesrsymtable : tsymtable;
  698. specializename,
  699. prettyname: ansistring;
  700. error : boolean;
  701. genname,
  702. ugenname : tidstring;
  703. module : tmodule;
  704. begin
  705. result:=false;
  706. if not assigned(genericparams) then
  707. exit;
  708. specializename:='$';
  709. prettyname:='';
  710. error:=false;
  711. for i:=0 to genericparams.count-1 do
  712. begin
  713. sym:=ttypesym(genericparams[i]);
  714. { ToDo: position }
  715. if not searchsym(upper(sym.RealName),typesrsym,typesrsymtable) then
  716. begin
  717. message1(sym_e_id_not_found,sym.name);
  718. error:=true;
  719. continue;
  720. end;
  721. if typesrsym.typ<>typesym then
  722. begin
  723. message(type_e_type_id_expected);
  724. error:=true;
  725. continue;
  726. end;
  727. module:=find_module_from_symtable(ttypesym(typesrsym).typedef.owner);
  728. if not assigned(module) then
  729. internalerror(2016112803);
  730. specializename:=specializename+'_$'+hexstr(module.moduleid,8)+'$$'+ttypesym(typesrsym).typedef.unique_id_str;
  731. if i>0 then
  732. prettyname:=prettyname+',';
  733. prettyname:=prettyname+ttypesym(typesrsym).prettyname;
  734. end;
  735. result:=true;
  736. if error then
  737. begin
  738. srsym:=generrorsym;
  739. exit;
  740. end;
  741. { ToDo: handle nested interfaces }
  742. genname:=generate_generic_name(sp,specializename,'');
  743. ugenname:=upper(genname);
  744. srsym:=search_object_name(ugenname,false);
  745. if not assigned(srsym) then
  746. begin
  747. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  748. srsym:=generrorsym;
  749. end;
  750. end;
  751. procedure specialize_generic_interface;
  752. var
  753. node : tnode;
  754. begin
  755. node:=factor(false,[ef_type_only,ef_had_specialize]);
  756. if node.nodetype=typen then
  757. begin
  758. sp:=ttypenode(node).typedef.typesym.name;
  759. end
  760. else
  761. sp:='';
  762. end;
  763. function check_generic_parameters(def:tstoreddef):boolean;
  764. var
  765. i : longint;
  766. decltype,
  767. impltype : ttypesym;
  768. implname : tsymstr;
  769. begin
  770. result:=true;
  771. if not assigned(def.genericparas) then
  772. internalerror(2018090102);
  773. if not assigned(genericparams) then
  774. internalerror(2018090103);
  775. if def.genericparas.count<>genericparams.count then
  776. internalerror(2018090104);
  777. for i:=0 to def.genericparas.count-1 do
  778. begin
  779. decltype:=ttypesym(def.genericparas[i]);
  780. impltype:=ttypesym(genericparams[i]);
  781. implname:=upper(genericparams.nameofindex(i));
  782. if decltype.name<>implname then
  783. begin
  784. messagepos1(impltype.fileinfo,sym_e_generic_type_param_mismatch,impltype.realname);
  785. messagepos1(decltype.fileinfo,sym_e_generic_type_param_decl,decltype.realname);
  786. result:=false;
  787. end;
  788. end;
  789. end;
  790. begin
  791. sp:='';
  792. orgsp:='';
  793. spnongen:='';
  794. orgspnongen:='';
  795. { Save the position where this procedure really starts }
  796. procstartfilepos:=current_tokenpos;
  797. old_parse_generic:=parse_generic;
  798. firstpart:=true;
  799. result:=false;
  800. pd:=nil;
  801. aprocsym:=nil;
  802. srsym:=nil;
  803. genericparams:=nil;
  804. hadspecialize:=false;
  805. addgendummy:=false;
  806. if not assigned(genericdef) then
  807. begin
  808. consume_proc_name;
  809. { examine interface map: function/procedure iname.functionname=locfuncname }
  810. if assigned(astruct) and
  811. (astruct.typ=objectdef) and
  812. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  813. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  814. (
  815. (token=_POINT) or
  816. (
  817. hadspecialize and
  818. (token=_ID)
  819. )
  820. ) then
  821. begin
  822. if hadspecialize and (token=_ID) then
  823. specialize_generic_interface;
  824. consume(_POINT);
  825. if hadspecialize or not handle_generic_interface then
  826. srsym:=search_object_name(sp,true);
  827. { qualifier is interface? }
  828. ImplIntf:=nil;
  829. if assigned(srsym) and
  830. (srsym.typ=typesym) and
  831. (ttypesym(srsym).typedef.typ=objectdef) then
  832. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  833. if ImplIntf=nil then
  834. begin
  835. Message(parser_e_interface_id_expected);
  836. { error recovery }
  837. consume(_ID);
  838. if try_to_consume(_EQ) then
  839. consume(_ID);
  840. exit;
  841. end
  842. else
  843. { in case of a generic or specialized interface we need to use the
  844. name of the def instead of the symbol, so that always the correct
  845. name is used }
  846. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  847. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  848. { must be a directly implemented interface }
  849. if Assigned(ImplIntf.ImplementsGetter) then
  850. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  851. consume(_ID);
  852. { Create unique name <interface>.<method> }
  853. hs:=sp+'.'+pattern;
  854. consume(_EQ);
  855. if assigned(ImplIntf) and
  856. (token=_ID) then
  857. ImplIntf.AddMapping(hs,pattern);
  858. consume(_ID);
  859. result:=true;
  860. exit;
  861. end;
  862. if assigned(genericparams) and assigned(current_genericdef) then
  863. Message(parser_f_no_generic_inside_generic);
  864. { method ? }
  865. srsym:=nil;
  866. if not assigned(astruct) and
  867. (symtablestack.top.symtablelevel=main_program_level) and
  868. try_to_consume(_POINT) then
  869. begin
  870. repeat
  871. classstartfilepos:=procstartfilepos;
  872. searchagain:=false;
  873. { throw the error at the right location }
  874. oldfilepos:=current_filepos;
  875. current_filepos:=procstartfilepos;
  876. if not assigned(astruct) and not assigned(srsym) then
  877. srsym:=search_object_name(sp,true);
  878. current_filepos:=oldfilepos;
  879. { we need to check whether the names of the generic parameter
  880. types match with the one in the declaration of a class/record,
  881. but we need to do this before consume_proc_name frees the
  882. type parameters of the class part }
  883. if (srsym.typ=typesym) and
  884. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) and
  885. tstoreddef(ttypesym(srsym).typedef).is_generic and
  886. assigned(genericparams) then
  887. { this is recoverable, so no further action necessary }
  888. check_generic_parameters(tstoreddef(ttypesym(srsym).typedef));
  889. { consume proc name }
  890. procstartfilepos:=current_tokenpos;
  891. consume_proc_name;
  892. { qualifier is class name ? }
  893. if (srsym.typ=typesym) and
  894. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  895. begin
  896. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  897. if (token<>_POINT) then
  898. if (potype in [potype_class_constructor,potype_class_destructor]) then
  899. sp:=lower(sp)
  900. else
  901. if (potype=potype_operator) and (optoken=NOTOKEN) then
  902. parse_operator_name;
  903. srsym:=tsym(astruct.symtable.Find(sp));
  904. if assigned(srsym) then
  905. begin
  906. if srsym.typ=procsym then
  907. aprocsym:=tprocsym(srsym)
  908. else
  909. if (srsym.typ=typesym) and
  910. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  911. begin
  912. searchagain:=true;
  913. consume(_POINT);
  914. end
  915. else
  916. begin
  917. { we use a different error message for tp7 so it looks more compatible }
  918. if (m_fpc in current_settings.modeswitches) then
  919. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  920. else
  921. Message(parser_e_methode_id_expected);
  922. { rename the name to an unique name to avoid an
  923. error when inserting the symbol in the symtable }
  924. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  925. end;
  926. end
  927. else
  928. begin
  929. MessagePos(procstartfilepos,parser_e_methode_id_expected);
  930. { recover by making it a normal procedure instead of method }
  931. astruct:=nil;
  932. end;
  933. end
  934. else
  935. MessagePos(classstartfilepos,parser_e_class_id_expected);
  936. until not searchagain;
  937. end
  938. else
  939. begin
  940. { check for constructor/destructor/class operators which are not allowed here }
  941. if (not parse_only) and
  942. ((potype in [potype_constructor,potype_destructor,
  943. potype_class_constructor,potype_class_destructor]) or
  944. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  945. Message(parser_e_only_methods_allowed);
  946. repeat
  947. { only 1 class constructor and destructor is allowed in the class and
  948. the check was already done with oo_has_class_constructor or
  949. oo_has_class_destructor -> skip searching
  950. (bug #28801) }
  951. if (potype in [potype_class_constructor,potype_class_destructor]) then
  952. break;
  953. searchagain:=false;
  954. current_tokenpos:=procstartfilepos;
  955. if (potype=potype_operator)and(optoken=NOTOKEN) then
  956. parse_operator_name;
  957. srsym:=tsym(symtablestack.top.Find(sp));
  958. { Also look in the globalsymtable if we didn't found
  959. the symbol in the localsymtable }
  960. if not assigned(srsym) and
  961. not(parse_only) and
  962. (symtablestack.top=current_module.localsymtable) and
  963. assigned(current_module.globalsymtable) then
  964. srsym:=tsym(current_module.globalsymtable.Find(sp));
  965. { Check if overloaded is a procsym }
  966. if assigned(srsym) then
  967. begin
  968. if srsym.typ=procsym then
  969. aprocsym:=tprocsym(srsym)
  970. else
  971. begin
  972. { when the other symbol is a unit symbol then hide the unit
  973. symbol, this is not supported in tp7 }
  974. if not(m_tp7 in current_settings.modeswitches) and
  975. (srsym.typ=unitsym) then
  976. begin
  977. HideSym(srsym);
  978. searchagain:=true;
  979. end
  980. else
  981. if (m_delphi in current_settings.modeswitches) and
  982. (srsym.typ=absolutevarsym) and
  983. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  984. begin
  985. HideSym(srsym);
  986. searchagain:=true;
  987. end
  988. else if (srsym.typ=typesym) and
  989. (sp_generic_dummy in srsym.symoptions) and
  990. (ttypesym(srsym).typedef.typ=undefineddef) then
  991. begin
  992. { this is a generic dummy symbol that has not yet
  993. been used; so we rename the dummy symbol and continue
  994. as if nothing happened }
  995. hidesym(srsym);
  996. searchagain:=true;
  997. addgendummy:=true;
  998. end
  999. else
  1000. begin
  1001. { we use a different error message for tp7 so it looks more compatible }
  1002. if (m_fpc in current_settings.modeswitches) then
  1003. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  1004. else
  1005. Message1(sym_e_duplicate_id,srsym.realname);
  1006. { rename the name to an unique name to avoid an
  1007. error when inserting the symbol in the symtable }
  1008. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  1009. end;
  1010. end;
  1011. end;
  1012. until not searchagain;
  1013. end;
  1014. { test again if assigned, it can be reset to recover }
  1015. if not assigned(aprocsym) then
  1016. begin
  1017. { create a new procsym and set the real filepos }
  1018. current_tokenpos:=procstartfilepos;
  1019. { for operator we have only one procsym for each overloaded
  1020. operation }
  1021. if (potype=potype_operator) then
  1022. begin
  1023. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  1024. if aprocsym=nil then
  1025. aprocsym:=cprocsym.create('$'+sp);
  1026. end
  1027. else
  1028. if (potype in [potype_class_constructor,potype_class_destructor]) then
  1029. aprocsym:=cprocsym.create('$'+lower(sp))
  1030. else
  1031. aprocsym:=cprocsym.create(orgsp);
  1032. if addgendummy then
  1033. include(aprocsym.symoptions,sp_generic_dummy);
  1034. symtablestack.top.insert(aprocsym);
  1035. end;
  1036. end;
  1037. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1038. st:=nil;
  1039. checkstack:=symtablestack.stack;
  1040. while assigned(checkstack) do
  1041. begin
  1042. st:=checkstack^.symtable;
  1043. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1044. break;
  1045. checkstack:=checkstack^.next;
  1046. end;
  1047. pd:=cprocdef.create(st.symtablelevel+1,not assigned(genericdef));
  1048. pd.struct:=astruct;
  1049. pd.procsym:=aprocsym;
  1050. pd.proctypeoption:=potype;
  1051. if assigned(genericparams) then
  1052. begin
  1053. include(pd.defoptions,df_generic);
  1054. { push the parameter symtable so that constraint definitions are added
  1055. there and not in the owner symtable }
  1056. symtablestack.push(pd.parast);
  1057. { register the parameters }
  1058. for i:=0 to genericparams.count-1 do
  1059. begin
  1060. ttypesym(genericparams[i]).register_sym;
  1061. tstoreddef(ttypesym(genericparams[i]).typedef).register_def;
  1062. end;
  1063. insert_generic_parameter_types(pd,nil,genericparams);
  1064. { the list is no longer required }
  1065. genericparams.free;
  1066. genericparams:=nil;
  1067. symtablestack.pop(pd.parast);
  1068. parse_generic:=true;
  1069. { also generate a dummy symbol if none exists already }
  1070. if assigned(astruct) then
  1071. dummysym:=tsym(astruct.symtable.find(spnongen))
  1072. else
  1073. begin
  1074. dummysym:=tsym(symtablestack.top.find(spnongen));
  1075. if not assigned(dummysym) and
  1076. (symtablestack.top=current_module.localsymtable) and
  1077. assigned(current_module.globalsymtable) then
  1078. dummysym:=tsym(current_module.globalsymtable.find(spnongen));
  1079. end;
  1080. if not assigned(dummysym) then
  1081. begin
  1082. dummysym:=ctypesym.create(orgspnongen,cundefineddef.create(true));
  1083. if assigned(astruct) then
  1084. astruct.symtable.insert(dummysym)
  1085. else
  1086. symtablestack.top.insert(dummysym);
  1087. end;
  1088. include(dummysym.symoptions,sp_generic_dummy);
  1089. { start token recorder for the declaration }
  1090. pd.init_genericdecl;
  1091. current_scanner.startrecordtokens(pd.genericdecltokenbuf);
  1092. end
  1093. else if assigned(genericdef) then
  1094. insert_generic_parameter_types(pd,tstoreddef(genericdef),generictypelist);
  1095. { methods inherit df_generic or df_specialization from the objectdef }
  1096. if assigned(pd.struct) and
  1097. (pd.parast.symtablelevel=normal_function_level) then
  1098. begin
  1099. if (df_generic in pd.struct.defoptions) then
  1100. begin
  1101. include(pd.defoptions,df_generic);
  1102. parse_generic:=true;
  1103. end;
  1104. if (df_specialization in pd.struct.defoptions) then
  1105. begin
  1106. if assigned(current_specializedef) then
  1107. begin
  1108. include(pd.defoptions,df_specialization);
  1109. { Find corresponding genericdef, we need it later to
  1110. replay the tokens to generate the body }
  1111. if not assigned(pd.struct.genericdef) then
  1112. internalerror(200512113);
  1113. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1114. if not assigned(genericst) then
  1115. internalerror(200512114);
  1116. { when searching for the correct procdef to use as genericdef we need to ignore
  1117. everything except procdefs so that we can find the correct indices }
  1118. index:=0;
  1119. found:=false;
  1120. for i:=0 to pd.owner.deflist.count-1 do
  1121. begin
  1122. if tdef(pd.owner.deflist[i]).typ<>procdef then
  1123. continue;
  1124. if pd.owner.deflist[i]=pd then
  1125. begin
  1126. found:=true;
  1127. break;
  1128. end;
  1129. inc(index);
  1130. end;
  1131. if not found then
  1132. internalerror(2014052301);
  1133. for i:=0 to genericst.deflist.count-1 do
  1134. begin
  1135. if tdef(genericst.deflist[i]).typ<>procdef then
  1136. continue;
  1137. if index=0 then
  1138. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  1139. dec(index);
  1140. end;
  1141. if not assigned(pd.genericdef) or
  1142. (pd.genericdef.typ<>procdef) then
  1143. internalerror(200512115);
  1144. end
  1145. else
  1146. Message(parser_e_explicit_method_implementation_for_specializations_not_allowed);
  1147. end;
  1148. end;
  1149. { methods need to be exported }
  1150. if assigned(astruct) and
  1151. (
  1152. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1153. (symtablestack.top.symtablelevel=main_program_level)
  1154. ) then
  1155. include(pd.procoptions,po_global);
  1156. { symbol options that need to be kept per procdef }
  1157. pd.fileinfo:=procstartfilepos;
  1158. pd.visibility:=symtablestack.top.currentvisibility;
  1159. if symtablestack.top.currentlyoptional then
  1160. include(pd.procoptions,po_optional);
  1161. { parse parameters }
  1162. if token=_LKLAMMER then
  1163. begin
  1164. old_current_structdef:=nil;
  1165. old_current_genericdef:=current_genericdef;
  1166. old_current_specializedef:=nil;
  1167. { Add ObjectSymtable to be able to find nested type definitions }
  1168. popclass:=0;
  1169. if assigned(pd.struct) and
  1170. (pd.parast.symtablelevel>=normal_function_level) and
  1171. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1172. begin
  1173. popclass:=push_nested_hierarchy(pd.struct);
  1174. old_current_structdef:=current_structdef;
  1175. old_current_specializedef:=current_specializedef;
  1176. current_structdef:=pd.struct;
  1177. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1178. current_genericdef:=current_structdef;
  1179. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1180. current_specializedef:=current_structdef;
  1181. end;
  1182. if pd.is_generic then
  1183. current_genericdef:=pd;
  1184. { Add parameter symtable }
  1185. if pd.parast.symtabletype<>staticsymtable then
  1186. symtablestack.push(pd.parast);
  1187. parse_parameter_dec(pd);
  1188. if pd.parast.symtabletype<>staticsymtable then
  1189. symtablestack.pop(pd.parast);
  1190. current_genericdef:=old_current_genericdef;
  1191. if popclass>0 then
  1192. begin
  1193. current_structdef:=old_current_structdef;
  1194. current_specializedef:=old_current_specializedef;
  1195. dec(popclass,pop_nested_hierarchy(pd.struct));
  1196. if popclass<>0 then
  1197. internalerror(201011260); // 11 nov 2010 index 0
  1198. end;
  1199. end;
  1200. parse_generic:=old_parse_generic;
  1201. result:=true;
  1202. end;
  1203. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean;astruct:tabstractrecorddef);
  1204. var
  1205. locationstr: string;
  1206. i: integer;
  1207. found: boolean;
  1208. procedure read_returndef(pd: tprocdef);
  1209. var
  1210. popclass: integer;
  1211. old_parse_generic: boolean;
  1212. old_current_structdef: tabstractrecorddef;
  1213. old_current_genericdef,
  1214. old_current_specializedef: tstoreddef;
  1215. begin
  1216. old_parse_generic:=parse_generic;
  1217. { Add ObjectSymtable to be able to find generic type definitions }
  1218. popclass:=0;
  1219. old_current_structdef:=nil;
  1220. old_current_genericdef:=current_genericdef;
  1221. old_current_specializedef:=current_specializedef;
  1222. current_genericdef:=nil;
  1223. current_specializedef:=nil;
  1224. if assigned(pd.struct) and
  1225. (pd.parast.symtablelevel>=normal_function_level) and
  1226. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1227. begin
  1228. popclass:=push_nested_hierarchy(pd.struct);
  1229. old_current_structdef:=current_structdef;
  1230. current_structdef:=pd.struct;
  1231. end;
  1232. if df_generic in pd.defoptions then
  1233. begin
  1234. if pd.is_generic then
  1235. current_genericdef:=pd
  1236. else if assigned(pd.struct) then
  1237. current_genericdef:=pd.struct
  1238. else
  1239. internalerror(2016090202);
  1240. end;
  1241. if df_specialization in pd.defoptions then
  1242. begin
  1243. if pd.is_specialization then
  1244. current_specializedef:=pd
  1245. else if assigned(pd.struct) then
  1246. current_specializedef:=pd.struct
  1247. else
  1248. internalerror(2016090203);
  1249. end;
  1250. parse_generic:=(df_generic in pd.defoptions);
  1251. if pd.is_generic or pd.is_specialization then
  1252. symtablestack.push(pd.parast);
  1253. single_type(pd.returndef,[stoAllowSpecialization]);
  1254. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1255. // testing and/or RTL patching.
  1256. {
  1257. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1258. not(cs_compilesystem in current_settings.moduleswitches) then
  1259. current_module.flags:=current_module.flags or uf_uses_variants;
  1260. }
  1261. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1262. Message1(type_e_not_automatable,pd.returndef.typename);
  1263. if pd.is_generic or pd.is_specialization then
  1264. symtablestack.pop(pd.parast);
  1265. if popclass>0 then
  1266. begin
  1267. current_structdef:=old_current_structdef;
  1268. dec(popclass,pop_nested_hierarchy(pd.struct));
  1269. if popclass<>0 then
  1270. internalerror(201012020);
  1271. end;
  1272. current_genericdef:=old_current_genericdef;
  1273. current_specializedef:=old_current_specializedef;
  1274. parse_generic:=old_parse_generic;
  1275. end;
  1276. begin
  1277. locationstr:='';
  1278. case pd.proctypeoption of
  1279. potype_procedure:
  1280. begin
  1281. pd.returndef:=voidtype;
  1282. if isclassmethod then
  1283. include(pd.procoptions,po_classmethod);
  1284. end;
  1285. potype_function:
  1286. begin
  1287. if try_to_consume(_COLON) then
  1288. begin
  1289. read_returndef(pd);
  1290. if (target_info.system in [system_m68k_amiga]) then
  1291. begin
  1292. if (idtoken=_LOCATION) then
  1293. begin
  1294. if po_explicitparaloc in pd.procoptions then
  1295. begin
  1296. consume(_LOCATION);
  1297. locationstr:=cstringpattern;
  1298. consume(_CSTRING);
  1299. end
  1300. else
  1301. { I guess this needs a new message... (KB) }
  1302. Message(parser_e_paraloc_all_paras);
  1303. end
  1304. else
  1305. begin
  1306. if po_explicitparaloc in pd.procoptions then
  1307. { assign default locationstr, if none specified }
  1308. { and we've arguments with explicit paraloc }
  1309. locationstr:='D0';
  1310. end;
  1311. end;
  1312. end
  1313. else
  1314. begin
  1315. if (
  1316. parse_only and
  1317. not(is_interface(pd.struct))
  1318. ) or
  1319. (m_repeat_forward in current_settings.modeswitches) then
  1320. begin
  1321. consume(_COLON);
  1322. consume_all_until(_SEMICOLON);
  1323. end;
  1324. end;
  1325. if isclassmethod then
  1326. include(pd.procoptions,po_classmethod);
  1327. end;
  1328. potype_constructor,
  1329. potype_class_constructor:
  1330. begin
  1331. if not isclassmethod and
  1332. assigned(pd) and
  1333. assigned(pd.struct) then
  1334. begin
  1335. { Set return type, class constructors return the
  1336. created instance, object constructors return boolean }
  1337. if is_class(pd.struct) or
  1338. is_record(pd.struct) or
  1339. is_javaclass(pd.struct) then
  1340. pd.returndef:=pd.struct
  1341. else
  1342. if is_objectpascal_helper(pd.struct) then
  1343. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1344. else
  1345. {$ifdef CPU64bitaddr}
  1346. pd.returndef:=bool64type;
  1347. {$else CPU64bitaddr}
  1348. pd.returndef:=bool32type;
  1349. {$endif CPU64bitaddr}
  1350. end
  1351. else
  1352. pd.returndef:=voidtype;
  1353. end;
  1354. potype_class_destructor,
  1355. potype_destructor:
  1356. begin
  1357. if assigned(pd) then
  1358. pd.returndef:=voidtype;
  1359. end;
  1360. potype_operator:
  1361. begin
  1362. { operators always need to be searched in all units (that
  1363. contain operators) }
  1364. include(pd.procoptions,po_overload);
  1365. pd.procsym.owner.includeoption(sto_has_operator);
  1366. if pd.parast.symtablelevel>normal_function_level then
  1367. Message(parser_e_no_local_operator);
  1368. if isclassmethod then
  1369. begin
  1370. include(pd.procoptions,po_classmethod);
  1371. { any class operator is also static }
  1372. include(pd.procoptions,po_staticmethod);
  1373. end;
  1374. if token<>_ID then
  1375. begin
  1376. if not(m_result in current_settings.modeswitches) then
  1377. consume(_ID);
  1378. end
  1379. else
  1380. begin
  1381. pd.resultname:=stringdup(orgpattern);
  1382. consume(_ID);
  1383. end;
  1384. { operators without result (management operators) }
  1385. if optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF, _OP_COPY] then
  1386. begin
  1387. { single var parameter to point the record }
  1388. if (optoken in [_OP_INITIALIZE, _OP_FINALIZE, _OP_ADDREF]) and
  1389. (
  1390. (pd.parast.SymList.Count<>1) or
  1391. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1392. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_var)
  1393. ) then
  1394. Message(parser_e_overload_impossible)
  1395. { constref (source) and var (dest) parameter to point the records }
  1396. else if (optoken=_OP_COPY) and
  1397. (
  1398. (pd.parast.SymList.Count<>2) or
  1399. (tparavarsym(pd.parast.SymList[0]).vardef<>pd.struct) or
  1400. (tparavarsym(pd.parast.SymList[0]).varspez<>vs_constref) or
  1401. (tparavarsym(pd.parast.SymList[1]).vardef<>pd.struct) or
  1402. (tparavarsym(pd.parast.SymList[1]).varspez<>vs_var)
  1403. ) then
  1404. Message(parser_e_overload_impossible);
  1405. trecordsymtable(pd.procsym.Owner).includemanagementoperator(
  1406. token2managementoperator(optoken));
  1407. pd.returndef:=voidtype
  1408. end
  1409. else
  1410. if not try_to_consume(_COLON) then
  1411. begin
  1412. consume(_COLON);
  1413. pd.returndef:=generrordef;
  1414. consume_all_until(_SEMICOLON);
  1415. end
  1416. else
  1417. begin
  1418. read_returndef(pd);
  1419. { check that class operators have either return type of structure or }
  1420. { at least one argument of that type }
  1421. if (po_classmethod in pd.procoptions) and
  1422. (pd.returndef <> pd.struct) then
  1423. begin
  1424. found:=false;
  1425. for i := 0 to pd.parast.SymList.Count - 1 do
  1426. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1427. begin
  1428. found:=true;
  1429. break;
  1430. end;
  1431. if not found then
  1432. if assigned(pd.struct) then
  1433. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1434. else
  1435. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1436. end;
  1437. if not assigned(pd.struct) or assigned(astruct) then
  1438. begin
  1439. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1440. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1441. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1442. message(parser_e_no_such_assignment)
  1443. else if not isoperatoracceptable(pd,optoken) then
  1444. Message(parser_e_overload_impossible);
  1445. end;
  1446. end;
  1447. end;
  1448. else
  1449. internalerror(2015052202);
  1450. end;
  1451. { file types can't be function results }
  1452. if assigned(pd) and
  1453. (pd.returndef.typ=filedef) then
  1454. message(parser_e_illegal_function_result);
  1455. { support procedure proc stdcall export; }
  1456. if not(check_proc_directive(false)) then
  1457. begin
  1458. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1459. begin
  1460. message(parser_e_field_not_allowed_here);
  1461. consume_all_until(_SEMICOLON);
  1462. end;
  1463. consume(_SEMICOLON);
  1464. end;
  1465. if locationstr<>'' then
  1466. begin
  1467. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1468. { I guess this needs a new message... (KB) }
  1469. message(parser_e_illegal_explicit_paraloc);
  1470. end;
  1471. end;
  1472. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  1473. var
  1474. pd : tprocdef;
  1475. old_block_type : tblock_type;
  1476. recover : boolean;
  1477. procedure finish_intf_mapping;
  1478. begin
  1479. if token=_COLON then
  1480. begin
  1481. message(parser_e_field_not_allowed_here);
  1482. consume_all_until(_SEMICOLON);
  1483. end;
  1484. consume(_SEMICOLON);
  1485. end;
  1486. begin
  1487. pd:=nil;
  1488. recover:=false;
  1489. case token of
  1490. _FUNCTION :
  1491. begin
  1492. consume(_FUNCTION);
  1493. if parse_proc_head(astruct,potype_function,isgeneric,nil,nil,pd) then
  1494. begin
  1495. { pd=nil when it is a interface mapping }
  1496. if assigned(pd) then
  1497. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1498. else
  1499. finish_intf_mapping;
  1500. end
  1501. else
  1502. begin
  1503. { recover }
  1504. consume(_COLON);
  1505. consume_all_until(_SEMICOLON);
  1506. recover:=true;
  1507. end;
  1508. end;
  1509. _PROCEDURE :
  1510. begin
  1511. consume(_PROCEDURE);
  1512. if parse_proc_head(astruct,potype_procedure,isgeneric,nil,nil,pd) then
  1513. begin
  1514. { pd=nil when it is an interface mapping }
  1515. if assigned(pd) then
  1516. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1517. else
  1518. finish_intf_mapping;
  1519. end
  1520. else
  1521. recover:=true;
  1522. end;
  1523. _CONSTRUCTOR :
  1524. begin
  1525. consume(_CONSTRUCTOR);
  1526. if isclassmethod then
  1527. recover:=not parse_proc_head(astruct,potype_class_constructor,false,nil,nil,pd)
  1528. else
  1529. recover:=not parse_proc_head(astruct,potype_constructor,false,nil,nil,pd);
  1530. if not recover then
  1531. parse_proc_dec_finish(pd,isclassmethod,astruct);
  1532. end;
  1533. _DESTRUCTOR :
  1534. begin
  1535. consume(_DESTRUCTOR);
  1536. if isclassmethod then
  1537. recover:=not parse_proc_head(astruct,potype_class_destructor,false,nil,nil,pd)
  1538. else
  1539. recover:=not parse_proc_head(astruct,potype_destructor,false,nil,nil,pd);
  1540. if not recover then
  1541. parse_proc_dec_finish(pd,isclassmethod,astruct);
  1542. end;
  1543. else
  1544. if (token=_OPERATOR) or
  1545. (isclassmethod and (idtoken=_OPERATOR)) then
  1546. begin
  1547. { we need to set the block type to bt_body, so that operator names
  1548. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1549. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1550. old_block_type:=block_type;
  1551. block_type:=bt_body;
  1552. consume(_OPERATOR);
  1553. parse_proc_head(astruct,potype_operator,false,nil,nil,pd);
  1554. block_type:=old_block_type;
  1555. if assigned(pd) then
  1556. parse_proc_dec_finish(pd,isclassmethod,astruct)
  1557. else
  1558. begin
  1559. { recover }
  1560. try_to_consume(_ID);
  1561. consume(_COLON);
  1562. consume_all_until(_SEMICOLON);
  1563. recover:=true;
  1564. end;
  1565. end;
  1566. end;
  1567. if recover and not(check_proc_directive(false)) then
  1568. begin
  1569. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1570. begin
  1571. message(parser_e_field_not_allowed_here);
  1572. consume_all_until(_SEMICOLON);
  1573. end;
  1574. consume(_SEMICOLON);
  1575. end;
  1576. { we've parsed the final semicolon, so stop recording tokens }
  1577. if assigned(pd) and
  1578. (df_generic in pd.defoptions) and
  1579. assigned(pd.genericdecltokenbuf) then
  1580. current_scanner.stoprecordtokens;
  1581. result:=pd;
  1582. end;
  1583. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1584. var
  1585. oldparse_only: boolean;
  1586. begin
  1587. oldparse_only:=parse_only;
  1588. parse_only:=true;
  1589. result:=parse_proc_dec(is_classdef,astruct,hadgeneric);
  1590. { this is for error recovery as well as forward }
  1591. { interface mappings, i.e. mapping to a method }
  1592. { which isn't declared yet }
  1593. if assigned(result) then
  1594. begin
  1595. parse_record_proc_directives(result);
  1596. { since records have no inheritance, don't allow non-static
  1597. class methods. Delphi does the same. }
  1598. if (result.proctypeoption<>potype_operator) and
  1599. is_classdef and
  1600. not (po_staticmethod in result.procoptions) then
  1601. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1602. // we can't add hidden params here because record is not yet defined
  1603. // and therefore record size which has influence on paramter passing rules may change too
  1604. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1605. handle_calling_convention(result,[hcc_declaration,hcc_check]);
  1606. { add definition to procsym }
  1607. proc_add_definition(result);
  1608. if result.is_generic then
  1609. astruct.symtable.includeoption(sto_has_generic);
  1610. end;
  1611. maybe_parse_hint_directives(result);
  1612. parse_only:=oldparse_only;
  1613. end;
  1614. {****************************************************************************
  1615. Procedure directive handlers
  1616. ****************************************************************************}
  1617. procedure pd_compilerproc(pd:tabstractprocdef);
  1618. var
  1619. v : Tconstexprint;
  1620. begin
  1621. { check for optional syssym index }
  1622. if try_to_consume(_COLON) then
  1623. begin
  1624. v:=get_intconst;
  1625. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1626. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1627. else if not assigned(tsyssym.find_by_number(longint(v.svalue))) then
  1628. message1(parser_e_invalid_internal_function_index,tostr(v))
  1629. else
  1630. tprocdef(pd).extnumber:=longint(v.svalue);
  1631. end;
  1632. end;
  1633. procedure pd_far(pd:tabstractprocdef);
  1634. begin
  1635. pd.declared_far;
  1636. end;
  1637. procedure pd_near(pd:tabstractprocdef);
  1638. begin
  1639. pd.declared_near;
  1640. end;
  1641. procedure pd_export(pd:tabstractprocdef);
  1642. begin
  1643. if pd.typ<>procdef then
  1644. internalerror(200304264);
  1645. if assigned(tprocdef(pd).struct) then
  1646. Message(parser_e_methods_dont_be_export);
  1647. if pd.parast.symtablelevel>normal_function_level then
  1648. Message(parser_e_dont_nest_export);
  1649. end;
  1650. procedure pd_forward(pd:tabstractprocdef);
  1651. begin
  1652. if pd.typ<>procdef then
  1653. internalerror(200304265);
  1654. tprocdef(pd).forwarddef:=true;
  1655. end;
  1656. procedure pd_alias(pd:tabstractprocdef);
  1657. begin
  1658. if pd.typ<>procdef then
  1659. internalerror(200304266);
  1660. consume(_COLON);
  1661. tprocdef(pd).aliasnames.insert(get_stringconst);
  1662. include(pd.procoptions,po_has_public_name);
  1663. end;
  1664. procedure pd_public(pd:tabstractprocdef);
  1665. begin
  1666. if pd.typ<>procdef then
  1667. internalerror(200304266);
  1668. if try_to_consume(_NAME) then
  1669. begin
  1670. tprocdef(pd).aliasnames.insert(get_stringconst);
  1671. include(pd.procoptions,po_has_public_name);
  1672. end;
  1673. end;
  1674. procedure pd_asmname(pd:tabstractprocdef);
  1675. begin
  1676. if pd.typ<>procdef then
  1677. internalerror(200304267);
  1678. if token=_CCHAR then
  1679. begin
  1680. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1681. consume(_CCHAR)
  1682. end
  1683. else
  1684. begin
  1685. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1686. consume(_CSTRING);
  1687. end;
  1688. { we don't need anything else }
  1689. tprocdef(pd).forwarddef:=false;
  1690. end;
  1691. procedure pd_internconst(pd:tabstractprocdef);
  1692. var v:Tconstexprint;
  1693. begin
  1694. if pd.typ<>procdef then
  1695. internalerror(200304268);
  1696. consume(_COLON);
  1697. v:=get_intconst;
  1698. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1699. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1700. else
  1701. Tprocdef(pd).extnumber:=longint(v.svalue);
  1702. end;
  1703. procedure pd_internproc(pd:tabstractprocdef);
  1704. var v:Tconstexprint;
  1705. begin
  1706. if pd.typ<>procdef then
  1707. internalerror(200304268);
  1708. consume(_COLON);
  1709. v:=get_intconst;
  1710. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1711. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1712. else
  1713. Tprocdef(pd).extnumber:=longint(v.svalue);
  1714. { the proc is defined }
  1715. tprocdef(pd).forwarddef:=false;
  1716. end;
  1717. procedure pd_interrupt(pd:tabstractprocdef);
  1718. begin
  1719. if pd.parast.symtablelevel>normal_function_level then
  1720. Message(parser_e_dont_nest_interrupt);
  1721. end;
  1722. procedure pd_abstract(pd:tabstractprocdef);
  1723. begin
  1724. if pd.typ<>procdef then
  1725. internalerror(200304269);
  1726. if is_objectpascal_helper(tprocdef(pd).struct) then
  1727. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1728. if assigned(tprocdef(pd).struct) and
  1729. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1730. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1731. else if (po_virtualmethod in pd.procoptions) then
  1732. begin
  1733. include(pd.procoptions,po_abstractmethod);
  1734. { one more abstract method }
  1735. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1736. end
  1737. else
  1738. Message(parser_e_only_virtual_methods_abstract);
  1739. { the method is defined }
  1740. tprocdef(pd).forwarddef:=false;
  1741. end;
  1742. procedure pd_final(pd:tabstractprocdef);
  1743. begin
  1744. if pd.typ<>procdef then
  1745. internalerror(200910170);
  1746. if is_objectpascal_helper(tprocdef(pd).struct) and
  1747. (m_objfpc in current_settings.modeswitches) then
  1748. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1749. if (po_virtualmethod in pd.procoptions) or
  1750. (is_javaclass(tprocdef(pd).struct) and
  1751. (po_classmethod in pd.procoptions)) then
  1752. include(pd.procoptions,po_finalmethod)
  1753. else
  1754. Message(parser_e_only_virtual_methods_final);
  1755. end;
  1756. procedure pd_enumerator(pd:tabstractprocdef);
  1757. begin
  1758. if pd.typ<>procdef then
  1759. internalerror(200910250);
  1760. if (token = _ID) then
  1761. begin
  1762. if pattern='MOVENEXT' then
  1763. begin
  1764. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1765. message(parser_e_only_one_enumerator_movenext);
  1766. pd.calcparas;
  1767. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1768. (pd.minparacount = 0) then
  1769. begin
  1770. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1771. include(pd.procoptions,po_enumerator_movenext);
  1772. end
  1773. else
  1774. Message(parser_e_enumerator_movenext_is_not_valid)
  1775. end
  1776. else
  1777. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1778. consume(token);
  1779. end
  1780. else
  1781. Message(parser_e_enumerator_identifier_required);
  1782. end;
  1783. procedure pd_virtual(pd:tabstractprocdef);
  1784. {$ifdef WITHDMT}
  1785. var
  1786. pt : tnode;
  1787. {$endif WITHDMT}
  1788. begin
  1789. if (not assigned(pd.owner.defowner) or
  1790. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1791. (po_external in pd.procoptions) then
  1792. Message2(parser_e_proc_dir_conflict,'EXTERNAL','"VIRTUAL"');
  1793. if pd.typ<>procdef then
  1794. internalerror(2003042610);
  1795. if (pd.proctypeoption=potype_constructor) and
  1796. is_object(tprocdef(pd).struct) then
  1797. Message(parser_e_constructor_cannot_be_not_virtual);
  1798. if pd.is_generic then
  1799. message(parser_e_genfuncs_cannot_be_virtual);
  1800. if is_objectpascal_helper(tprocdef(pd).struct) and
  1801. (m_objfpc in current_settings.modeswitches) then
  1802. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1803. {$ifdef WITHDMT}
  1804. if is_object(tprocdef(pd).struct) and
  1805. (token<>_SEMICOLON) then
  1806. begin
  1807. { any type of parameter is allowed here! }
  1808. pt:=comp_expr(true);
  1809. if is_constintnode(pt) then
  1810. begin
  1811. include(pd.procoptions,po_msgint);
  1812. pd.messageinf.i:=pt.value;
  1813. end
  1814. else
  1815. Message(parser_e_ill_msg_expr);
  1816. disposetree(pt);
  1817. end;
  1818. {$endif WITHDMT}
  1819. end;
  1820. procedure pd_dispid(pd:tabstractprocdef);
  1821. var pt:Tnode;
  1822. begin
  1823. if pd.typ<>procdef then
  1824. internalerror(200604301);
  1825. pt:=comp_expr([ef_accept_equal]);
  1826. if is_constintnode(pt) then
  1827. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1828. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1829. else
  1830. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1831. else
  1832. message(parser_e_dispid_must_be_ord_const);
  1833. pt.free;
  1834. end;
  1835. procedure pd_static(pd:tabstractprocdef);
  1836. begin
  1837. if pd.typ<>procdef then
  1838. internalerror(2013032001);
  1839. if not assigned(tprocdef(pd).struct) then
  1840. internalerror(2013032002);
  1841. include(tprocdef(pd).procsym.symoptions,sp_static);
  1842. { "static" is not allowed for operators or normal methods (except in objects) }
  1843. if (pd.proctypeoption=potype_operator) or
  1844. (
  1845. not (po_classmethod in pd.procoptions) and
  1846. not is_object(tprocdef(pd).struct)
  1847. )
  1848. then
  1849. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1850. include(pd.procoptions,po_staticmethod);
  1851. end;
  1852. procedure pd_override(pd:tabstractprocdef);
  1853. begin
  1854. if pd.typ<>procdef then
  1855. internalerror(2003042611);
  1856. if is_objectpascal_helper(tprocdef(pd).struct) then
  1857. begin
  1858. if m_objfpc in current_settings.modeswitches then
  1859. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1860. end
  1861. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1862. Message(parser_e_no_object_override)
  1863. else if is_objccategory(tprocdef(pd).struct) then
  1864. Message(parser_e_no_category_override)
  1865. else if (po_external in pd.procoptions) and
  1866. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1867. not is_cppclass(tprocdef(pd).struct) and
  1868. not is_java_class_or_interface(tprocdef(pd).struct) then
  1869. Message2(parser_e_proc_dir_conflict,'OVERRIDE','"EXTERNAL"');
  1870. end;
  1871. procedure pd_overload(pd:tabstractprocdef);
  1872. begin
  1873. if pd.typ<>procdef then
  1874. internalerror(2003042612);
  1875. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1876. end;
  1877. procedure pd_message(pd:tabstractprocdef);
  1878. var
  1879. pt : tnode;
  1880. paracnt : longint;
  1881. begin
  1882. if pd.typ<>procdef then
  1883. internalerror(2003042613);
  1884. if is_objectpascal_helper(tprocdef(pd).struct) then
  1885. begin
  1886. if m_objfpc in current_settings.modeswitches then
  1887. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1888. end
  1889. else
  1890. if not is_class(tprocdef(pd).struct) and
  1891. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1892. Message(parser_e_msg_only_for_classes);
  1893. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1894. Message(parser_e_multiple_messages);
  1895. { check parameter type }
  1896. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1897. begin
  1898. if po_external in pd.procoptions then
  1899. Message2(parser_e_proc_dir_conflict,'MESSAGE','"EXTERNAL"');
  1900. paracnt:=0;
  1901. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1902. if paracnt<>1 then
  1903. Message(parser_e_ill_msg_param);
  1904. end;
  1905. pt:=comp_expr([ef_accept_equal]);
  1906. { message is 1-character long }
  1907. if is_constcharnode(pt) then
  1908. begin
  1909. include(pd.procoptions,po_msgstr);
  1910. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1911. end
  1912. else if pt.nodetype=stringconstn then
  1913. begin
  1914. include(pd.procoptions,po_msgstr);
  1915. if (tstringconstnode(pt).len>255) then
  1916. Message(parser_e_message_string_too_long);
  1917. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1918. end
  1919. else
  1920. if is_constintnode(pt) and
  1921. (is_class(tprocdef(pd).struct) or
  1922. is_objectpascal_helper(tprocdef(pd).struct)) then
  1923. begin
  1924. include(pd.procoptions,po_msgint);
  1925. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1926. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1927. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1928. else
  1929. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1930. end
  1931. else
  1932. Message(parser_e_ill_msg_expr);
  1933. { check whether the selector name is valid in case of Objective-C }
  1934. if (po_msgstr in pd.procoptions) and
  1935. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1936. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1937. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1938. pt.free;
  1939. end;
  1940. procedure pd_reintroduce(pd:tabstractprocdef);
  1941. begin
  1942. if pd.typ<>procdef then
  1943. internalerror(200401211);
  1944. if is_objectpascal_helper(tprocdef(pd).struct) then
  1945. begin
  1946. if m_objfpc in current_settings.modeswitches then
  1947. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1948. end
  1949. else
  1950. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1951. not(is_objccategory(tprocdef(pd).struct)) and
  1952. not(is_javaclass(tprocdef(pd).struct)) then
  1953. Message(parser_e_no_object_reintroduce);
  1954. end;
  1955. procedure pd_syscall(pd:tabstractprocdef);
  1956. procedure include_po_syscall;
  1957. var
  1958. syscall: psyscallinfo;
  1959. begin
  1960. case target_info.system of
  1961. system_arm_palmos,
  1962. system_m68k_palmos,
  1963. system_m68k_atari,
  1964. system_m68k_amiga,
  1965. system_powerpc_amiga:
  1966. include(pd.procoptions,get_default_syscall);
  1967. system_powerpc_morphos,
  1968. system_arm_aros,
  1969. system_i386_aros,
  1970. system_x86_64_aros:
  1971. begin
  1972. syscall:=get_syscall_by_token(idtoken);
  1973. if assigned(syscall) then
  1974. begin
  1975. if target_info.system in syscall^.validon then
  1976. begin
  1977. consume(idtoken);
  1978. include(pd.procoptions,syscall^.procoption);
  1979. end
  1980. end
  1981. else
  1982. include(pd.procoptions,get_default_syscall);
  1983. end;
  1984. end;
  1985. end;
  1986. function po_syscall_to_varoptions: tvaroptions;
  1987. begin
  1988. result:=[vo_is_syscall_lib,vo_is_hidden_para];
  1989. if ([po_syscall_legacy,po_syscall_basereg,po_syscall_basenone] * tprocdef(pd).procoptions) <> [] then
  1990. include(result,vo_has_explicit_paraloc);
  1991. end;
  1992. function po_syscall_to_regname: string;
  1993. begin
  1994. if po_syscall_legacy in tprocdef(pd).procoptions then
  1995. result:='a6'
  1996. { let nobase on MorphOS store the libbase in r12 as well, because
  1997. we will need the libbase anyway during the call generation }
  1998. else if (po_syscall_basenone in tprocdef(pd).procoptions) and
  1999. (target_info.system = system_powerpc_morphos) then
  2000. result:='r12'
  2001. else if po_syscall_basereg in tprocdef(pd).procoptions then
  2002. begin
  2003. case target_info.system of
  2004. system_i386_aros:
  2005. result:='eax';
  2006. system_x86_64_aros:
  2007. result:='rax';
  2008. system_powerpc_morphos:
  2009. result:='r12';
  2010. else
  2011. internalerror(2016090201);
  2012. end;
  2013. end
  2014. else
  2015. internalerror(2016090101);
  2016. end;
  2017. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2018. const
  2019. syscall_paranr: array[boolean] of aint =
  2020. ( paranr_syscall_lib_last, paranr_syscall_lib_first );
  2021. var
  2022. vs : tparavarsym;
  2023. sym : tsym;
  2024. symtable : TSymtable;
  2025. v: Tconstexprint;
  2026. vo: tvaroptions;
  2027. paranr: aint;
  2028. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2029. begin
  2030. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  2031. internalerror(2003042614);
  2032. tprocdef(pd).forwarddef:=false;
  2033. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2034. include_po_syscall;
  2035. if target_info.system in [system_arm_palmos, system_m68k_palmos] then
  2036. begin
  2037. v:=get_intconst;
  2038. tprocdef(pd).extnumber:=longint(v.svalue);
  2039. if ((v<0) or (v>high(word))) then
  2040. message(parser_e_range_check_error);
  2041. if try_to_consume(_COMMA) then
  2042. begin
  2043. v:=get_intconst;
  2044. if ((v<0) or (v>high(word))) then
  2045. message(parser_e_range_check_error);
  2046. tprocdef(pd).import_nr:=longint(v.svalue);
  2047. include(pd.procoptions,po_syscall_has_importnr);
  2048. end;
  2049. exit;
  2050. end;
  2051. if target_info.system = system_m68k_atari then
  2052. begin
  2053. v:=get_intconst;
  2054. if ((v<0) or (v>15)) then
  2055. message(parser_e_range_check_error)
  2056. else
  2057. tprocdef(pd).extnumber:=longint(v.svalue);
  2058. v:=get_intconst;
  2059. if ((v<0) or (v>high(smallint))) then
  2060. message(parser_e_range_check_error)
  2061. else
  2062. tprocdef(pd).import_nr:=longint(v.svalue);
  2063. exit;
  2064. end;
  2065. if consume_sym(sym,symtable) then
  2066. if (sym.typ=staticvarsym) and
  2067. ((tabstractvarsym(sym).vardef.typ=pointerdef) or
  2068. is_32bitint(tabstractvarsym(sym).vardef)) then
  2069. begin
  2070. include(pd.procoptions,po_syscall_has_libsym);
  2071. tcpuprocdef(pd).libsym:=sym;
  2072. vo:=po_syscall_to_varoptions;
  2073. paranr:=syscall_paranr[po_syscall_basefirst in tprocdef(pd).procoptions];
  2074. vs:=cparavarsym.create('$syscalllib',paranr,vs_value,tabstractvarsym(sym).vardef,vo);
  2075. if vo_has_explicit_paraloc in vo then
  2076. if not paramanager.parseparaloc(vs,po_syscall_to_regname) then
  2077. internalerror(2016120301);
  2078. pd.parast.insert(vs);
  2079. end
  2080. else
  2081. Message(parser_e_32bitint_or_pointer_variable_expected);
  2082. paramanager.create_funcretloc_info(pd,calleeside);
  2083. paramanager.create_funcretloc_info(pd,callerside);
  2084. v:=get_intconst;
  2085. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2086. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  2087. else
  2088. if target_info.system in [system_arm_aros,system_i386_aros,system_x86_64_aros] then
  2089. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint)
  2090. else
  2091. Tprocdef(pd).extnumber:=v.uvalue;
  2092. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2093. end;
  2094. procedure pd_external(pd:tabstractprocdef);
  2095. {
  2096. If import_dll=nil the procedure is assumed to be in another
  2097. object file. In that object file it should have the name to
  2098. which import_name is pointing to. Otherwise, the procedure is
  2099. assumed to be in the DLL to which import_dll is pointing to. In
  2100. that case either import_nr<>0 or import_name<>nil is true, so
  2101. the procedure is either imported by number or by name. (DM)
  2102. }
  2103. var
  2104. hs : string;
  2105. v:Tconstexprint;
  2106. is_java_external: boolean;
  2107. begin
  2108. if pd.typ<>procdef then
  2109. internalerror(2003042615);
  2110. { Allow specifying a separate external name for methods in external Java
  2111. because its identifier naming constraints are laxer than FPC's
  2112. (e.g., case sensitive).
  2113. Limitation: only allows specifying the symbol name and not the package name,
  2114. and only for external classes/interfaces }
  2115. is_java_external:=
  2116. (pd.typ=procdef) and
  2117. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2118. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2119. with tprocdef(pd) do
  2120. begin
  2121. forwarddef:=false;
  2122. { forbid local external procedures }
  2123. if parast.symtablelevel>normal_function_level then
  2124. Message(parser_e_no_local_proc_external);
  2125. { If the procedure should be imported from a DLL, a constant string follows.
  2126. This isn't really correct, an contant string expression follows
  2127. so we check if an semicolon follows, else a string constant have to
  2128. follow (FK) }
  2129. if not is_java_external and
  2130. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2131. begin
  2132. { Always add library prefix and suffix to create an uniform name }
  2133. hs:=get_stringconst;
  2134. if ExtractFileExt(hs)='' then
  2135. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2136. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2137. hs:=target_info.sharedlibprefix+hs;
  2138. { the JVM expects java/lang/Object rather than java.lang.Object }
  2139. if target_info.system in systems_jvm then
  2140. Replace(hs,'.','/');
  2141. import_dll:=stringdup(hs);
  2142. include(procoptions,po_has_importdll);
  2143. if (idtoken=_NAME) then
  2144. begin
  2145. consume(_NAME);
  2146. import_name:=stringdup(get_stringconst);
  2147. include(procoptions,po_has_importname);
  2148. if import_name^='' then
  2149. message(parser_e_empty_import_name);
  2150. end;
  2151. if (idtoken=_INDEX) then
  2152. begin
  2153. {After the word index follows the index number in the DLL.}
  2154. consume(_INDEX);
  2155. v:=get_intconst;
  2156. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2157. message(parser_e_range_check_error)
  2158. else
  2159. import_nr:=longint(v.svalue);
  2160. end;
  2161. { default is to used the realname of the procedure }
  2162. if (import_nr=0) and not assigned(import_name) then
  2163. begin
  2164. import_name:=stringdup(procsym.realname);
  2165. include(procoptions,po_has_importname);
  2166. end;
  2167. end
  2168. else
  2169. begin
  2170. if (idtoken=_NAME) or
  2171. is_java_external then
  2172. begin
  2173. consume(_NAME);
  2174. import_name:=stringdup(get_stringconst);
  2175. include(procoptions,po_has_importname);
  2176. if import_name^='' then
  2177. message(parser_e_empty_import_name);
  2178. end;
  2179. end;
  2180. end;
  2181. end;
  2182. procedure pd_weakexternal(pd:tabstractprocdef);
  2183. begin
  2184. if not(target_info.system in systems_weak_linking) then
  2185. message(parser_e_weak_external_not_supported)
  2186. else
  2187. pd_external(pd);
  2188. end;
  2189. procedure pd_winapi(pd:tabstractprocdef);
  2190. begin
  2191. if not(target_info.system in systems_all_windows+[system_i386_nativent]) then
  2192. pd.proccalloption:=pocall_cdecl
  2193. else
  2194. pd.proccalloption:=pocall_stdcall;
  2195. include(pd.procoptions,po_hascallingconvention);
  2196. end;
  2197. procedure pd_hardfloat(pd:tabstractprocdef);
  2198. begin
  2199. if
  2200. {$if defined(arm)}
  2201. (current_settings.fputype=fpu_soft) or
  2202. {$endif defined(arm)}
  2203. (cs_fp_emulation in current_settings.moduleswitches) then
  2204. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2205. end;
  2206. type
  2207. pd_handler=procedure(pd:tabstractprocdef);
  2208. proc_dir_rec=record
  2209. idtok : ttoken;
  2210. pd_flags : tpdflags;
  2211. handler : pd_handler;
  2212. pocall : tproccalloption;
  2213. pooption : tprocoptions;
  2214. mutexclpocall : tproccalloptions;
  2215. mutexclpotype : tproctypeoptions;
  2216. mutexclpo : tprocoptions;
  2217. end;
  2218. const
  2219. {Should contain the number of procedure directives we support.}
  2220. num_proc_directives=52;
  2221. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2222. (
  2223. (
  2224. idtok:_ABSTRACT;
  2225. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2226. handler : @pd_abstract;
  2227. pocall : pocall_none;
  2228. pooption : [po_abstractmethod];
  2229. mutexclpocall : [pocall_internproc];
  2230. mutexclpotype : [];
  2231. mutexclpo : [po_exports,po_interrupt,po_inline]
  2232. ),(
  2233. idtok:_ALIAS;
  2234. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2235. handler : @pd_alias;
  2236. pocall : pocall_none;
  2237. pooption : [];
  2238. mutexclpocall : [];
  2239. mutexclpotype : [];
  2240. mutexclpo : [po_external,po_inline]
  2241. ),(
  2242. idtok:_ASMNAME;
  2243. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2244. handler : @pd_asmname;
  2245. pocall : pocall_cdecl;
  2246. pooption : [po_external];
  2247. mutexclpocall : [pocall_internproc];
  2248. mutexclpotype : [];
  2249. mutexclpo : [po_external,po_inline]
  2250. ),(
  2251. idtok:_ASSEMBLER;
  2252. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2253. handler : nil;
  2254. pocall : pocall_none;
  2255. pooption : [po_assembler];
  2256. mutexclpocall : [];
  2257. mutexclpotype : [];
  2258. mutexclpo : [po_external]
  2259. ),(
  2260. idtok:_C; {same as cdecl for mode mac}
  2261. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2262. handler : nil;
  2263. pocall : pocall_cdecl;
  2264. pooption : [];
  2265. mutexclpocall : [];
  2266. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2267. mutexclpo : [po_assembler,po_external]
  2268. ),(
  2269. idtok:_CBLOCK;
  2270. pd_flags : [pd_procvar];
  2271. handler : nil;
  2272. pocall : pocall_none;
  2273. pooption : [po_is_block];
  2274. mutexclpocall : [];
  2275. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2276. mutexclpo : [po_assembler,po_external]
  2277. ),(
  2278. idtok:_CDECL;
  2279. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2280. handler : nil;
  2281. pocall : pocall_cdecl;
  2282. pooption : [];
  2283. mutexclpocall : [];
  2284. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2285. mutexclpo : [po_assembler,po_external]
  2286. ),(
  2287. idtok:_DISPID;
  2288. pd_flags : [pd_dispinterface];
  2289. handler : @pd_dispid;
  2290. pocall : pocall_none;
  2291. pooption : [po_dispid];
  2292. mutexclpocall : [pocall_internproc];
  2293. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2294. mutexclpo : [po_interrupt,po_external,po_inline]
  2295. ),(
  2296. idtok:_DYNAMIC;
  2297. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2298. handler : @pd_virtual;
  2299. pocall : pocall_none;
  2300. pooption : [po_virtualmethod];
  2301. mutexclpocall : [pocall_internproc];
  2302. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2303. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2304. ),(
  2305. idtok:_EXPORT;
  2306. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2307. handler : @pd_export;
  2308. pocall : pocall_none;
  2309. pooption : [po_exports,po_global];
  2310. mutexclpocall : [pocall_internproc];
  2311. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2312. mutexclpo : [po_external,po_interrupt,po_inline]
  2313. ),(
  2314. idtok:_EXTERNAL;
  2315. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2316. handler : @pd_external;
  2317. pocall : pocall_none;
  2318. pooption : [po_external];
  2319. mutexclpocall : [pocall_syscall];
  2320. { allowed for external cpp classes }
  2321. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2322. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2323. ),(
  2324. idtok:_FAR;
  2325. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2326. handler : @pd_far;
  2327. pocall : pocall_none;
  2328. pooption : [];
  2329. mutexclpocall : [pocall_internproc];
  2330. mutexclpotype : [];
  2331. mutexclpo : [po_inline]
  2332. ),(
  2333. idtok:_FAR16;
  2334. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2335. handler : nil;
  2336. pocall : pocall_far16;
  2337. pooption : [];
  2338. mutexclpocall : [];
  2339. mutexclpotype : [];
  2340. mutexclpo : [po_external]
  2341. ),(
  2342. idtok:_FINAL;
  2343. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2344. handler : @pd_final;
  2345. pocall : pocall_none;
  2346. pooption : [po_finalmethod];
  2347. mutexclpocall : [pocall_internproc];
  2348. mutexclpotype : [];
  2349. mutexclpo : [po_exports,po_interrupt,po_inline]
  2350. ),(
  2351. idtok:_FORWARD;
  2352. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2353. handler : @pd_forward;
  2354. pocall : pocall_none;
  2355. pooption : [];
  2356. mutexclpocall : [pocall_internproc];
  2357. mutexclpotype : [];
  2358. mutexclpo : [po_external,po_inline]
  2359. ),(
  2360. idtok:_OLDFPCCALL;
  2361. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2362. handler : nil;
  2363. pocall : pocall_oldfpccall;
  2364. pooption : [];
  2365. mutexclpocall : [];
  2366. mutexclpotype : [];
  2367. mutexclpo : []
  2368. ),(
  2369. idtok:_INLINE;
  2370. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2371. handler : nil;
  2372. pocall : pocall_none;
  2373. pooption : [po_inline];
  2374. mutexclpocall : [pocall_safecall];
  2375. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2376. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2377. ),(
  2378. idtok:_INTERNCONST;
  2379. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2380. handler : @pd_internconst;
  2381. pocall : pocall_none;
  2382. pooption : [po_internconst];
  2383. mutexclpocall : [];
  2384. mutexclpotype : [potype_operator];
  2385. mutexclpo : []
  2386. ),(
  2387. idtok:_INTERNPROC;
  2388. pd_flags : [pd_interface,pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2389. handler : @pd_internproc;
  2390. pocall : pocall_internproc;
  2391. pooption : [];
  2392. mutexclpocall : [];
  2393. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2394. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2395. ),(
  2396. idtok:_INTERRUPT;
  2397. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2398. handler : @pd_interrupt;
  2399. {$ifdef i386}
  2400. pocall : pocall_oldfpccall;
  2401. {$else i386}
  2402. pocall : pocall_stdcall;
  2403. {$endif i386}
  2404. pooption : [po_interrupt];
  2405. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2406. pocall_pascal,pocall_far16,pocall_oldfpccall,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2407. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2408. mutexclpo : [po_external,po_inline,po_exports]
  2409. ),(
  2410. idtok:_IOCHECK;
  2411. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2412. handler : nil;
  2413. pocall : pocall_none;
  2414. pooption : [po_iocheck];
  2415. mutexclpocall : [pocall_internproc];
  2416. mutexclpotype : [];
  2417. mutexclpo : [po_external]
  2418. ),(
  2419. idtok:_LOCAL;
  2420. pd_flags : [pd_implemen,pd_body];
  2421. handler : nil;
  2422. pocall : pocall_none;
  2423. pooption : [po_kylixlocal];
  2424. mutexclpocall : [pocall_internproc,pocall_far16];
  2425. mutexclpotype : [];
  2426. mutexclpo : [po_external,po_exports]
  2427. ),(
  2428. idtok:_MESSAGE;
  2429. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2430. handler : @pd_message;
  2431. pocall : pocall_none;
  2432. pooption : []; { can be po_msgstr or po_msgint }
  2433. mutexclpocall : [pocall_internproc];
  2434. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2435. mutexclpo : [po_interrupt,po_inline]
  2436. ),(
  2437. idtok:_MWPASCAL;
  2438. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2439. handler : nil;
  2440. pocall : pocall_mwpascal;
  2441. pooption : [];
  2442. mutexclpocall : [];
  2443. mutexclpotype : [];
  2444. mutexclpo : []
  2445. ),(
  2446. idtok:_NEAR;
  2447. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2448. handler : @pd_near;
  2449. pocall : pocall_none;
  2450. pooption : [];
  2451. mutexclpocall : [pocall_internproc];
  2452. mutexclpotype : [];
  2453. mutexclpo : []
  2454. ),(
  2455. idtok:_NORETURN;
  2456. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2457. handler : nil;
  2458. pocall : pocall_none;
  2459. pooption : [po_noreturn];
  2460. mutexclpocall : [];
  2461. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2462. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2463. ),(
  2464. idtok:_NOSTACKFRAME;
  2465. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2466. handler : nil;
  2467. pocall : pocall_none;
  2468. pooption : [po_nostackframe];
  2469. mutexclpocall : [pocall_internproc];
  2470. mutexclpotype : [];
  2471. mutexclpo : []
  2472. ),(
  2473. idtok:_OVERLOAD;
  2474. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2475. handler : @pd_overload;
  2476. pocall : pocall_none;
  2477. pooption : [po_overload];
  2478. mutexclpocall : [pocall_internproc];
  2479. mutexclpotype : [];
  2480. mutexclpo : []
  2481. ),(
  2482. idtok:_OVERRIDE;
  2483. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2484. handler : @pd_override;
  2485. pocall : pocall_none;
  2486. pooption : [po_overridingmethod,po_virtualmethod];
  2487. mutexclpocall : [pocall_internproc];
  2488. mutexclpotype : [];
  2489. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2490. ),(
  2491. idtok:_PASCAL;
  2492. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2493. handler : nil;
  2494. pocall : pocall_pascal;
  2495. pooption : [];
  2496. mutexclpocall : [];
  2497. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2498. mutexclpo : [po_external]
  2499. ),(
  2500. idtok:_PUBLIC;
  2501. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2502. handler : @pd_public;
  2503. pocall : pocall_none;
  2504. pooption : [po_public,po_global];
  2505. mutexclpocall : [pocall_internproc];
  2506. mutexclpotype : [];
  2507. mutexclpo : [po_external,po_inline]
  2508. ),(
  2509. idtok:_REGISTER;
  2510. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2511. handler : nil;
  2512. pocall : pocall_register;
  2513. pooption : [];
  2514. mutexclpocall : [];
  2515. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2516. mutexclpo : [po_external]
  2517. ),(
  2518. idtok:_REINTRODUCE;
  2519. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2520. handler : @pd_reintroduce;
  2521. pocall : pocall_none;
  2522. pooption : [po_reintroduce];
  2523. mutexclpocall : [pocall_internproc];
  2524. mutexclpotype : [];
  2525. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2526. ),(
  2527. idtok:_SAFECALL;
  2528. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2529. handler : nil;
  2530. pocall : pocall_safecall;
  2531. pooption : [];
  2532. mutexclpocall : [];
  2533. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2534. mutexclpo : [po_external]
  2535. ),(
  2536. idtok:_SOFTFLOAT;
  2537. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2538. handler : nil;
  2539. pocall : pocall_softfloat;
  2540. pooption : [];
  2541. mutexclpocall : [];
  2542. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2543. { it's available with po_external because the libgcc floating point routines on the arm
  2544. uses this calling convention }
  2545. mutexclpo : []
  2546. ),(
  2547. idtok:_STATIC;
  2548. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2549. handler : @pd_static;
  2550. pocall : pocall_none;
  2551. pooption : [po_staticmethod];
  2552. mutexclpocall : [pocall_internproc];
  2553. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2554. mutexclpo : [po_interrupt,po_exports,po_virtualmethod]
  2555. ),(
  2556. idtok:_STDCALL;
  2557. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2558. handler : nil;
  2559. pocall : pocall_stdcall;
  2560. pooption : [];
  2561. mutexclpocall : [];
  2562. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2563. mutexclpo : [po_external]
  2564. ),(
  2565. idtok:_SYSCALL;
  2566. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2567. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2568. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2569. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2570. handler : @pd_syscall;
  2571. pocall : pocall_syscall;
  2572. pooption : [];
  2573. mutexclpocall : [];
  2574. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2575. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2576. ),(
  2577. idtok:_VIRTUAL;
  2578. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2579. handler : @pd_virtual;
  2580. pocall : pocall_none;
  2581. pooption : [po_virtualmethod];
  2582. mutexclpocall : [pocall_internproc];
  2583. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2584. mutexclpo : PD_VIRTUAL_MUTEXCLPO
  2585. ),(
  2586. idtok:_CPPDECL;
  2587. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2588. handler : nil;
  2589. pocall : pocall_cppdecl;
  2590. pooption : [];
  2591. mutexclpocall : [];
  2592. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2593. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2594. ),(
  2595. idtok:_VARARGS;
  2596. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2597. handler : nil;
  2598. pocall : pocall_none;
  2599. pooption : [po_varargs];
  2600. mutexclpocall : [pocall_internproc,pocall_register,
  2601. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2602. mutexclpotype : [];
  2603. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2604. ),(
  2605. idtok:_COMPILERPROC;
  2606. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2607. handler : @pd_compilerproc;
  2608. pocall : pocall_none;
  2609. pooption : [po_compilerproc];
  2610. mutexclpocall : [];
  2611. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2612. mutexclpo : [po_interrupt]
  2613. ),(
  2614. idtok:_WEAKEXTERNAL;
  2615. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2616. handler : @pd_weakexternal;
  2617. pocall : pocall_none;
  2618. { mark it both external and weak external, so we don't have to
  2619. adapt all code for external symbols to also check for weak external
  2620. }
  2621. pooption : [po_external,po_weakexternal];
  2622. mutexclpocall : [pocall_internproc,pocall_syscall];
  2623. { allowed for external cpp classes }
  2624. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2625. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2626. ),(
  2627. idtok:_WINAPI;
  2628. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2629. handler : @pd_winapi;
  2630. pocall : pocall_none;
  2631. pooption : [];
  2632. mutexclpocall : [pocall_stdcall,pocall_cdecl,pocall_mwpascal,pocall_sysv_abi_cdecl,pocall_ms_abi_cdecl];
  2633. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2634. mutexclpo : [po_external]
  2635. ),(
  2636. idtok:_ENUMERATOR;
  2637. pd_flags : [pd_interface,pd_object,pd_record];
  2638. handler : @pd_enumerator;
  2639. pocall : pocall_none;
  2640. pooption : [];
  2641. mutexclpocall : [pocall_internproc];
  2642. mutexclpotype : [];
  2643. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2644. ),(
  2645. idtok:_RTLPROC;
  2646. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2647. handler : nil;
  2648. pocall : pocall_none;
  2649. pooption : [po_rtlproc];
  2650. mutexclpocall : [];
  2651. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2652. mutexclpo : [po_interrupt]
  2653. ),(
  2654. idtok:_HARDFLOAT;
  2655. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2656. handler : @pd_hardfloat;
  2657. pocall : pocall_hardfloat;
  2658. pooption : [];
  2659. mutexclpocall : [];
  2660. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2661. { it's available with po_external because the libgcc floating point routines on the arm
  2662. uses this calling convention }
  2663. mutexclpo : []
  2664. ),(
  2665. idtok:_SYSV_ABI_DEFAULT;
  2666. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2667. handler : nil;
  2668. pocall : pocall_sysv_abi_default;
  2669. pooption : [];
  2670. mutexclpocall : [];
  2671. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2672. mutexclpo : [po_interrupt]
  2673. ),(
  2674. idtok:_SYSV_ABI_CDECL;
  2675. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2676. handler : nil;
  2677. pocall : pocall_sysv_abi_cdecl;
  2678. pooption : [];
  2679. mutexclpocall : [];
  2680. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2681. mutexclpo : [po_interrupt]
  2682. ),(
  2683. idtok:_MS_ABI_DEFAULT;
  2684. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2685. handler : nil;
  2686. pocall : pocall_ms_abi_default;
  2687. pooption : [];
  2688. mutexclpocall : [];
  2689. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2690. mutexclpo : [po_interrupt]
  2691. ),(
  2692. idtok:_MS_ABI_CDECL;
  2693. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2694. handler : nil;
  2695. pocall : pocall_ms_abi_cdecl;
  2696. pooption : [];
  2697. mutexclpocall : [];
  2698. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2699. mutexclpo : [po_interrupt]
  2700. ),(
  2701. idtok:_VECTORCALL;
  2702. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2703. handler : nil;
  2704. pocall : pocall_vectorcall;
  2705. pooption : [];
  2706. mutexclpocall : [];
  2707. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2708. mutexclpo : [po_interrupt]
  2709. )
  2710. );
  2711. function check_proc_directive(isprocvar:boolean):boolean;
  2712. var
  2713. i : longint;
  2714. begin
  2715. result:=false;
  2716. for i:=1 to num_proc_directives do
  2717. if proc_direcdata[i].idtok=idtoken then
  2718. begin
  2719. if ((not isprocvar) or
  2720. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2721. { don't eat a public directive in classes }
  2722. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2723. result:=true;
  2724. exit;
  2725. end;
  2726. end;
  2727. function find_proc_directive_index(tok: ttoken): longint; inline;
  2728. begin
  2729. result:=-1;
  2730. for result:=1 to num_proc_directives do
  2731. if proc_direcdata[result].idtok=tok then
  2732. exit;
  2733. result:=-1;
  2734. end;
  2735. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2736. {
  2737. Parse the procedure directive, returns true if a correct directive is found
  2738. }
  2739. var
  2740. p : longint;
  2741. name,
  2742. other : TIDString;
  2743. po_comp : tprocoptions;
  2744. tokenloc : TFilePosInfo;
  2745. begin
  2746. parse_proc_direc:=false;
  2747. name:=tokeninfo^[idtoken].str;
  2748. { Hint directive? Then exit immediatly }
  2749. if (m_hintdirective in current_settings.modeswitches) then
  2750. begin
  2751. case idtoken of
  2752. _LIBRARY,
  2753. _PLATFORM,
  2754. _UNIMPLEMENTED,
  2755. _EXPERIMENTAL,
  2756. _DEPRECATED :
  2757. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2758. begin
  2759. maybe_parse_hint_directives(tprocdef(pd));
  2760. { could the new token still be a directive? }
  2761. if token<>_ID then
  2762. exit;
  2763. end
  2764. else
  2765. exit;
  2766. end;
  2767. end;
  2768. { C directive is MacPas only, because it breaks too much existing code
  2769. on other platforms (PFV) }
  2770. if (idtoken=_C) and
  2771. not(m_mac in current_settings.modeswitches) then
  2772. exit;
  2773. { retrieve data for directive if found }
  2774. p:=find_proc_directive_index(idtoken);
  2775. { Check if the procedure directive is known }
  2776. if p=-1 then
  2777. begin
  2778. { parsing a procvar type the name can be any
  2779. next variable !! }
  2780. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2781. not(idtoken in [_PROPERTY,_GENERIC]) then
  2782. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2783. exit;
  2784. end;
  2785. { check if method and directive not for object, like public.
  2786. This needs to be checked also for procvars }
  2787. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2788. (symtablestack.top.symtabletype=ObjectSymtable) and
  2789. { directive allowed for cpp classes? }
  2790. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2791. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2792. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2793. exit;
  2794. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2795. (symtablestack.top.symtabletype=recordsymtable) then
  2796. exit;
  2797. { check if method and directive not for java class }
  2798. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2799. is_javaclass(tdef(symtablestack.top.defowner)) then
  2800. exit;
  2801. { check if method and directive not for java interface }
  2802. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2803. is_javainterface(tdef(symtablestack.top.defowner)) then
  2804. exit;
  2805. { Keep track of the token's position in the file so it's correctly indicated if an error occurs. }
  2806. tokenloc := current_tokenpos;
  2807. { consume directive, and turn flag on }
  2808. consume(token);
  2809. parse_proc_direc:=true;
  2810. { Conflicts between directives? }
  2811. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) then
  2812. begin
  2813. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,ProcTypeOptionKeywords[pd.proctypeoption]);
  2814. exit;
  2815. end;
  2816. if (pd.proccalloption in proc_direcdata[p].mutexclpocall) then
  2817. begin
  2818. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,'"' + UpCase(proccalloptionStr[pd.proccalloption]) + '"');
  2819. exit;
  2820. end;
  2821. po_comp := (pd.procoptions*proc_direcdata[p].mutexclpo);
  2822. if (po_comp<>[]) then
  2823. begin
  2824. MessagePos2(tokenloc, parser_e_proc_dir_conflict,name,get_first_proc_str(po_comp));
  2825. exit;
  2826. end;
  2827. { set calling convention }
  2828. if proc_direcdata[p].pocall<>pocall_none then
  2829. begin
  2830. if (po_hascallingconvention in pd.procoptions) then
  2831. begin
  2832. MessagePos2(tokenloc, parser_w_proc_overriding_calling,
  2833. proccalloptionStr[pd.proccalloption],
  2834. proccalloptionStr[proc_direcdata[p].pocall]);
  2835. end;
  2836. { check if the target processor supports this calling convention }
  2837. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2838. begin
  2839. MessagePos1(tokenloc, parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2840. { recover }
  2841. proc_direcdata[p].pocall:=pocall_stdcall;
  2842. end;
  2843. pd.proccalloption:=proc_direcdata[p].pocall;
  2844. include(pd.procoptions,po_hascallingconvention);
  2845. end;
  2846. if pd.typ=procdef then
  2847. begin
  2848. { Check if the directive is only for objects }
  2849. if (pd_object in proc_direcdata[p].pd_flags) and
  2850. not assigned(tprocdef(pd).struct) then
  2851. exit;
  2852. { Check if the directive is only for records }
  2853. if (pd_record in proc_direcdata[p].pd_flags) and
  2854. not assigned(tprocdef(pd).struct) then
  2855. exit;
  2856. { check if method and directive not for interface }
  2857. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2858. is_interface(tprocdef(pd).struct) then
  2859. exit;
  2860. { check if method and directive not for interface }
  2861. if is_dispinterface(tprocdef(pd).struct) and
  2862. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2863. exit;
  2864. { check if method and directive not for objcclass }
  2865. if is_objcclass(tprocdef(pd).struct) and
  2866. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2867. exit;
  2868. { check if method and directive not for objcprotocol }
  2869. if is_objcprotocol(tprocdef(pd).struct) and
  2870. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2871. exit;
  2872. { check if method and directive not for record/class helper }
  2873. if is_objectpascal_helper(tprocdef(pd).struct) and
  2874. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2875. exit;
  2876. end;
  2877. { Check the pd_flags if the directive should be allowed }
  2878. if (pd_interface in pdflags) and
  2879. not(pd_interface in proc_direcdata[p].pd_flags) then
  2880. begin
  2881. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_interface,name);
  2882. exit;
  2883. end;
  2884. if (pd_implemen in pdflags) and
  2885. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2886. begin
  2887. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_implementation,name);
  2888. exit;
  2889. end;
  2890. if (pd_procvar in pdflags) and
  2891. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2892. begin
  2893. MessagePos1(tokenloc, parser_e_proc_dir_not_allowed_in_procvar,name);
  2894. exit;
  2895. end;
  2896. { Return the new pd_flags }
  2897. if not(pd_body in proc_direcdata[p].pd_flags) then
  2898. exclude(pdflags,pd_body);
  2899. { Add the correct flag }
  2900. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2901. { Call the handler }
  2902. if pointer(proc_direcdata[p].handler)<>nil then
  2903. proc_direcdata[p].handler(pd);
  2904. end;
  2905. function proc_get_importname(pd:tprocdef):string;
  2906. var
  2907. dllname, importname : string;
  2908. begin
  2909. result:='';
  2910. if not(po_external in pd.procoptions) then
  2911. internalerror(200412151);
  2912. { external name or number is specified }
  2913. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2914. begin
  2915. if assigned(pd.import_dll) then
  2916. dllname:=pd.import_dll^
  2917. else
  2918. dllname:='';
  2919. if assigned(pd.import_name) then
  2920. importname:=pd.import_name^
  2921. else
  2922. importname:='';
  2923. proc_get_importname:=make_dllmangledname(dllname,
  2924. importname,pd.import_nr,pd.proccalloption);
  2925. end
  2926. else
  2927. begin
  2928. { Default names when importing variables }
  2929. case pd.proccalloption of
  2930. pocall_cdecl,
  2931. pocall_sysv_abi_cdecl,
  2932. pocall_ms_abi_cdecl:
  2933. begin
  2934. if assigned(pd.struct) then
  2935. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2936. else
  2937. result:=target_info.Cprefix+pd.procsym.realname;
  2938. end;
  2939. pocall_cppdecl :
  2940. begin
  2941. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2942. end;
  2943. else
  2944. begin
  2945. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2946. { but according to MacPas mode description
  2947. Cprefix should still be used PM }
  2948. if (m_mac in current_settings.modeswitches) then
  2949. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  2950. else
  2951. result:=pd.procsym.realname;
  2952. end;
  2953. end;
  2954. end;
  2955. end;
  2956. procedure proc_set_mangledname(pd:tprocdef);
  2957. var
  2958. s : string;
  2959. begin
  2960. { When the mangledname is already set we aren't allowed to change
  2961. it because it can already be used somewhere (PFV) }
  2962. if not(po_has_mangledname in pd.procoptions) then
  2963. begin
  2964. if (po_external in pd.procoptions) then
  2965. begin
  2966. { External Procedures are only allowed to change the mangledname
  2967. in their first declaration }
  2968. if (pd.forwarddef or (not pd.hasforward)) then
  2969. begin
  2970. s:=proc_get_importname(pd);
  2971. if s<>'' then
  2972. begin
  2973. pd.setmangledname(s);
  2974. end;
  2975. { since this is an external declaration, there won't be an
  2976. implementation that needs to match the original symbol
  2977. again -> immediately convert here }
  2978. if po_compilerproc in pd.procoptions then
  2979. pd.setcompilerprocname;
  2980. end
  2981. end
  2982. else
  2983. { Normal procedures }
  2984. begin
  2985. if (po_compilerproc in pd.procoptions) then
  2986. begin
  2987. pd.setmangledname(lower(pd.procsym.name));
  2988. end;
  2989. end;
  2990. end;
  2991. { Public/exported alias names }
  2992. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2993. not(po_has_public_name in pd.procoptions) then
  2994. begin
  2995. case pd.proccalloption of
  2996. pocall_cdecl,
  2997. pocall_sysv_abi_cdecl,
  2998. pocall_ms_abi_cdecl:
  2999. begin
  3000. if assigned(pd.struct) then
  3001. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  3002. else
  3003. begin
  3004. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  3005. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  3006. (po_exports in pd.procoptions) then
  3007. pd.aliasnames.insert(pd.procsym.realname)
  3008. else
  3009. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  3010. end;
  3011. end;
  3012. pocall_cppdecl :
  3013. begin
  3014. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  3015. end;
  3016. end;
  3017. { prevent adding the alias a second time }
  3018. include(pd.procoptions,po_has_public_name);
  3019. end;
  3020. end;
  3021. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  3022. {
  3023. Parse the procedure directives. It does not matter if procedure directives
  3024. are written using ;procdir; or ['procdir'] syntax.
  3025. }
  3026. var
  3027. stoprecording,
  3028. res : boolean;
  3029. begin
  3030. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  3031. begin
  3032. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  3033. include(pd.procoptions,po_public);
  3034. include(pd.procoptions,po_has_public_name);
  3035. include(pd.procoptions,po_global);
  3036. end;
  3037. { methods from external class definitions are all external themselves }
  3038. if (pd.typ=procdef) and
  3039. assigned(tprocdef(pd).struct) and
  3040. (tprocdef(pd).struct.typ=objectdef) and
  3041. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3042. tprocdef(pd).make_external;
  3043. { Class constructors and destructor are static class methods in real. }
  3044. { There are many places in the compiler where either class or static }
  3045. { method flag changes the behavior. It is simplier to add them to }
  3046. { the class constructors/destructors options than to fix all the }
  3047. { occurencies. (Paul) }
  3048. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3049. begin
  3050. include(pd.procoptions,po_classmethod);
  3051. include(pd.procoptions,po_staticmethod);
  3052. end;
  3053. { for a generic routine we also need to record the procedure }
  3054. { directives, but only if we aren't already recording for a }
  3055. { surrounding generic }
  3056. if pd.is_generic and (pd.typ=procdef) and not current_scanner.is_recording_tokens then
  3057. begin
  3058. current_scanner.startrecordtokens(tprocdef(pd).genericdecltokenbuf);
  3059. stoprecording:=true;
  3060. end
  3061. else
  3062. stoprecording:=false;
  3063. while token in [_ID,_LECKKLAMMER] do
  3064. begin
  3065. if try_to_consume(_LECKKLAMMER) then
  3066. begin
  3067. repeat
  3068. parse_proc_direc(pd,pdflags);
  3069. until not try_to_consume(_COMMA);
  3070. consume(_RECKKLAMMER);
  3071. { we always expect at least '[];' }
  3072. res:=true;
  3073. end
  3074. else
  3075. begin
  3076. res:=parse_proc_direc(pd,pdflags);
  3077. end;
  3078. { A procedure directive normally followed by a semicolon, but in
  3079. a const section or reading a type we should stop when _EQ is found,
  3080. because a constant/default value follows }
  3081. if res then
  3082. begin
  3083. if (block_type=bt_const_type) and
  3084. (token=_EQ) then
  3085. break;
  3086. { support procedure proc;stdcall export; }
  3087. if not(check_proc_directive((pd.typ=procvardef))) then
  3088. begin
  3089. { support "record p : procedure stdcall end;" and
  3090. "var p : procedure stdcall = nil;" }
  3091. if (pd_procvar in pdflags) and
  3092. (token in [_END,_RKLAMMER,_EQ]) then
  3093. break
  3094. else
  3095. begin
  3096. if (token=_COLON) then
  3097. begin
  3098. Message(parser_e_field_not_allowed_here);
  3099. consume_all_until(_SEMICOLON);
  3100. end;
  3101. consume(_SEMICOLON)
  3102. end;
  3103. end;
  3104. end
  3105. else
  3106. break;
  3107. end;
  3108. if stoprecording then
  3109. current_scanner.stoprecordtokens;
  3110. { nostackframe requires assembler, but assembler
  3111. may be specified in the implementation part only,
  3112. and in not required if the function is first forward declared
  3113. if it is a procdef that has forwardef set to true
  3114. we postpone the possible error message to the real implementation
  3115. parse_only does not need to be considered as po_nostackframe
  3116. is an implementation only directive }
  3117. if (po_nostackframe in pd.procoptions) and
  3118. not (po_assembler in pd.procoptions) and
  3119. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3120. message(parser_e_nostackframe_without_assembler);
  3121. end;
  3122. procedure parse_var_proc_directives(sym:tsym);
  3123. var
  3124. pdflags : tpdflags;
  3125. pd : tabstractprocdef;
  3126. begin
  3127. pdflags:=[pd_procvar];
  3128. pd:=nil;
  3129. case sym.typ of
  3130. fieldvarsym,
  3131. staticvarsym,
  3132. localvarsym,
  3133. paravarsym :
  3134. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  3135. typesym :
  3136. pd:=tabstractprocdef(ttypesym(sym).typedef);
  3137. else
  3138. internalerror(2003042617);
  3139. end;
  3140. if pd.typ<>procvardef then
  3141. internalerror(2003042618);
  3142. { names should never be used anyway }
  3143. parse_proc_directives(pd,pdflags);
  3144. end;
  3145. procedure parse_object_proc_directives(pd:tabstractprocdef);
  3146. var
  3147. pdflags : tpdflags;
  3148. begin
  3149. pdflags:=[pd_object];
  3150. parse_proc_directives(pd,pdflags);
  3151. end;
  3152. procedure parse_record_proc_directives(pd:tabstractprocdef);
  3153. var
  3154. pdflags : tpdflags;
  3155. begin
  3156. pdflags:=[pd_record];
  3157. parse_proc_directives(pd,pdflags);
  3158. end;
  3159. end.