pdecsub.pas 131 KB

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