pdecsub.pas 126 KB

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